Back to Repositories

Testing Module Resolution Patterns in Create-React-App

This test suite validates module loading functionality in Create React App using absolute paths with baseUrl configuration. It focuses on verifying proper component rendering and module resolution within the React application environment.

Test Coverage Overview

The test coverage focuses on validating absolute module imports using the baseUrl configuration in Create React App. It ensures proper module resolution and component rendering by:

  • Testing successful component mounting with absolute imports
  • Verifying async component loading behavior
  • Validating baseUrl path resolution

Implementation Analysis

The testing approach utilizes Jest and React Testing Library patterns for component validation. It implements an asynchronous testing pattern using Promises to handle component mounting and ready states.

The test leverages React’s rendering lifecycle and DOM manipulation through ReactDOM.render(), combined with Promise-based resolution for async operations.

Technical Details

  • Testing Framework: Jest
  • React Testing Dependencies: ReactDOM
  • Test Environment: jsdom
  • Configuration: baseUrl in jsconfig.json
  • Component Under Test: App.js

Best Practices Demonstrated

The test demonstrates several React testing best practices including proper component cleanup, async testing patterns, and isolated component testing.

  • Async/await pattern usage
  • Component isolation
  • Promise-based resolution
  • Clean DOM manipulation

facebook/create-react-app

test/fixtures/jsconfig/src/App.test.js

            
/**
 * Copyright (c) 2015-present, Facebook, Inc.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';

test('loads modules absolutely with baseUrl', () => {
  const div = document.createElement('div');
  return new Promise(resolve => {
    ReactDOM.render(<App onReady={resolve} />, div);
  });
});