jest.config.ts
Overview
The jest.config.ts file provides the Jest testing framework configuration tailored for a project using the Umi framework. It exports an asynchronous default function that constructs and returns a Jest configuration object (Config.InitialOptions), leveraging utilities from the umi/test package.
This configuration is optimized for browser-targeted testing with the esbuild JavaScript transformer and includes custom setup files, coverage collection rules, and coverage thresholds. It also prepares the configuration to handle potential ES module npm packages if needed.
Detailed Explanation
Default Exported Async Function
export default async () => { ... };
Purpose: Generates and returns a Jest configuration object asynchronously.
Returns: A Promise resolving to
Config.InitialOptions— the shape expected by Jest for its configuration.
Imported Elements
Config: Provides type definitions and interfaces related to Jest config.configUmiAlias: A utility to apply Umi-specific alias configurations to Jest.createConfig: A function to create a base Jest configuration with specified parameters.
Configuration Construction Workflow
Base Configuration Creation (
createConfig)createConfig({ target: 'browser', jsTransformer: 'esbuild', jsTransformerOpts: { jsx: 'automatic' }, })target: 'browser': Configures Jest to simulate a browser environment.jsTransformer: 'esbuild': Usesesbuildas the JS/TS transformer for fast compilation.jsTransformerOpts: { jsx: 'automatic' }: Passes options toesbuildfor JSX transformation, set to automatic mode (React 17+ JSX transform).
Adding Additional Jest Options
Extends the base config with:
setupFilesAfterEnv: Points to a setup filejest-setup.tsto run post-environment initialization.collectCoverageFrom: Defines glob patterns to specify which files to collect coverage from, excluding build, config, mock, and generated directories.coverageThreshold: Sets a minimal global coverage threshold (lines >= 1).Commented
transformIgnorePatterns: Guidance for transforming ES module packages innode_modulesif needed.
Applying Umi Alias (
configUmiAlias)The combined configuration is passed to
configUmiAlias, which adjusts module resolution aliases so that Jest can resolve imports according to Umi's aliasing rules.Type Assertion
The final result is asserted as
Config.InitialOptionsfor type safety.
Usage Example
import jestConfig from './jest.config';
(async () => {
const config = await jestConfig();
console.log(config);
// Use `config` as Jest config in your test runner setup
})();
In the context of Umi projects, this exported async function is typically referenced in Jest CLI commands or scripts to generate the Jest config dynamically.
Important Implementation Details
Asynchronous Export: The config is generated asynchronously, possibly because
configUmiAliasorcreateConfigmay perform asynchronous operations (e.g., reading project files or resolving aliases).esbuildas Transformer: Opting foresbuildaccelerates test compilation and supports modern JavaScript and TypeScript features, including JSX with the new automatic runtime.Coverage Collection and Exclusion: Coverage is collected from most source files but specifically excludes:
Umi build and test output folders (
.umi,.umi-test,.umi-production)Umi configuration files
Jest config file itself
Coverage and dist folders
Mock and config directories
Coverage Threshold: A minimal line coverage threshold is set to 1% globally, acting as a placeholder or a simple check to ensure coverage data is generated.
Commented Section for ESM Packages: There is a commented-out option to customize
transformIgnorePatternsfor ES module packages that require transformation by Jest, which is useful when importing such packages directly.
Interaction with Other Parts of the System
Umi Framework: Integrates with Umi's aliasing and configuration system via
configUmiAliasandcreateConfigfrom'umi/test'.Test Setup File: References a Jest setup file at
<rootDir>/jest-setup.tswhere additional global test setup can be done (e.g., configuring testing libraries, mocks).Jest CLI/Test Runner: The exported config is consumed by Jest when running tests, allowing seamless integration within the Umi project environment.
Source Code & Coverage Tools: Enables coverage collection and enforces coverage thresholds for project source files while excluding irrelevant or generated files.
Mermaid Diagram: Flowchart of Configuration Construction
flowchart TD
A[Start: Async Default Exported Function] --> B[Call createConfig]
B --> C[Base Jest Config for Browser + esbuild]
C --> D[Extend with Additional Jest Options]
D --> E[Pass to configUmiAlias]
E --> F[Apply Umi Aliasing to Config]
F --> G[Return Final Config as Config.InitialOptions]
style A fill:#f9f,stroke:#333,stroke-width:2px
style G fill:#bbf,stroke:#333,stroke-width:2px
Summary
This file is a specialized Jest configuration generator designed for Umi projects. It creates a Jest config object tailored for browser testing with fast esbuild transformation, integrates Umi's module aliasing, sets up environment initialization, and configures code coverage collection and thresholds. The asynchronous design and modular use of Umi testing utilities make it flexible and maintainable within the broader Umi ecosystem.