Jest testing utilities and configurations for Umi applications with TypeScript/JavaScript transform support
npx @tessl/cli install tessl/npm-umijs--test@4.4.0@umijs/test provides Jest testing utilities and configurations specifically designed for Umi applications. It offers pre-configured Jest setups with support for multiple JavaScript transformers (esbuild, swc, ts-jest), TypeScript/JSX transformation, CSS module mocking, and both Node.js and browser testing environments.
npm install @umijs/testimport { createConfig, JSTransformer } from "@umijs/test";
import type { Config } from "@umijs/test";For CommonJS:
const { createConfig } = require("@umijs/test");import { createConfig } from "@umijs/test";
// Basic Jest configuration with default esbuild transformer
const jestConfig = createConfig();
// Configuration with specific transformer
const jestConfigWithSwc = createConfig({
jsTransformer: 'swc',
target: 'node'
});
// Browser environment configuration
const browserConfig = createConfig({
jsTransformer: 'esbuild',
target: 'browser',
jsTransformerOpts: {
target: 'es2020'
}
});
export default jestConfig;@umijs/test is built around several key components:
createConfig function generates complete Jest configurations with Umi-specific defaultsCreates a pre-configured Jest configuration optimized for Umi applications with sensible defaults and transformer support.
/**
* Creates a Jest configuration with Umi-specific defaults
* @param opts - Configuration options
* @returns Complete Jest configuration object
*/
function createConfig(opts?: {
/** JavaScript transformer to use - defaults to 'esbuild' */
jsTransformer?: JSTransformer;
/** Runtime environment - defaults to 'node' */
target?: 'node' | 'browser';
/** Additional options passed to the JS transformer */
jsTransformerOpts?: any;
}): Config.InitialOptions;Configuration Defaults:
The createConfig function provides these pre-configured settings:
**/*.test.(t|j)s(x)? - Matches test files with .test extensionidentity-obj-proxytarget: 'browser'Usage Examples:
// Default configuration with esbuild
const defaultConfig = createConfig();
// Swc transformer for faster builds
const swcConfig = createConfig({
jsTransformer: 'swc'
});
// Browser environment with jsdom
const browserConfig = createConfig({
target: 'browser',
jsTransformer: 'esbuild',
jsTransformerOpts: {
target: 'es2020',
sourcemap: true
}
});
// ts-jest for maximum TypeScript compatibility
const tsJestConfig = createConfig({
jsTransformer: 'ts-jest'
});/** Supported JavaScript/TypeScript transformers */
type JSTransformer = 'esbuild' | 'swc' | 'ts-jest';
/** Jest configuration interface (re-exported from @jest/types) */
interface Config {
// Complete Jest configuration type from @jest/types
InitialOptions: {
testMatch?: string[];
transform?: Record<string, any>;
moduleNameMapper?: Record<string, string>;
testTimeout?: number;
transformIgnorePatterns?: string[];
setupFiles?: string[];
resolver?: string;
testEnvironment?: string;
// ... and all other Jest configuration options
};
}The package includes several internal utilities that power the main API:
Custom Jest transformer using esbuild for fast TypeScript/JavaScript compilation:
.ts, .tsx, .js, .jsx, .mjs filesjest.mock() calls via Babel transformationjsTransformerOpts parameterJest resolver that modifies package.json resolution:
main field over module fieldAutomatic environment setup:
isomorphic-unfetch for fetch API support in Node.js test environmentsThe package handles several common testing scenarios:
jsTransformer valuesFor advanced use cases, you can extend the returned configuration:
import { createConfig } from "@umijs/test";
const baseConfig = createConfig({
jsTransformer: 'esbuild',
target: 'node'
});
// Extend with custom settings
const customConfig = {
...baseConfig,
collectCoverage: true,
coverageDirectory: 'coverage',
coverageReporters: ['text', 'lcov'],
setupFilesAfterEnv: ['<rootDir>/test-setup.ts']
};
export default customConfig;