A Jest-based testing utility package for Umi.js applications that provides pre-configured test framework setup and transformers.
npx @tessl/cli install tessl/npm-umi-test@1.0.0umi-test is a Jest-based testing utility library specifically designed for Umi.js applications. It provides pre-configured Jest settings with custom transformers for JavaScript and TypeScript, automatic test file discovery, CSS/LESS module mocking, and integrated setup files for React component testing with Enzyme.
npm install umi-testconst test = require("umi-test").default;For ES modules:
import test from "umi-test";const test = require("umi-test").default;
// Run tests with default configuration
test().then(() => {
console.log('Tests passed');
}).catch(e => {
console.error('Tests failed:', e);
});
// Run tests with custom options
test({
watch: true,
coverage: true,
cwd: '/path/to/project',
moduleNameMapper: {
'^@/(.*)$': '<rootDir>/src/$1'
}
});CLI usage:
# Run tests once
umi-test
# Run in watch mode
umi-test --watch
# Run with coverage
umi-test --coverageumi-test is built around several key components:
Core test runner function that configures and runs Jest with Umi-specific settings including transformers, setup files, and module resolution.
/**
* Main test runner function for Umi.js applications
* @param {Object} [opts={}] - Configuration options for Jest and test execution
* @param {string} [opts.cwd] - Current working directory (defaults to process.cwd())
* @param {Object} [opts.moduleNameMapper] - Additional module name mappings for Jest
* @param {boolean} [opts.watch] - Enable watch mode
* @param {boolean} [opts.coverage] - Enable coverage reporting
* @returns {Promise<void>} Promise that resolves on successful test completion or rejects on failure
*/
function test(opts = {});Usage Examples:
const test = require("umi-test").default;
// Basic usage
await test();
// With custom configuration
await test({
cwd: '/path/to/project',
watch: true,
coverage: true,
moduleNameMapper: {
'^@components/(.*)$': '<rootDir>/src/components/$1',
'^@utils/(.*)$': '<rootDir>/src/utils/$1'
}
});Command-line interface for running tests with common options.
# Usage: umi-test [options]
# Options:
# -w, --watch Enable watch mode
# --coverage Enable coverage reporting
# Additional Jest CLI options are passed through to JestUsage Examples:
# Run tests once
umi-test
# Run in watch mode
umi-test --watch
# Run with coverage
umi-test --coverage
# Combine options
umi-test --watch --coverageCustom transformers for processing JavaScript, TypeScript, and JSX files.
// JavaScript/JSX Transformer (babel-jest with babel-preset-umi)
const jsTransformer;
// TypeScript/TSX Transformer (ts-jest)
const tsTransformer;
// JS Transformer Configuration:
// - Uses babel-preset-umi with transformRuntime: false
// - Includes babel-plugin-module-resolver with aliases:
// - ts-jest, react, react-dom, enzymeThe test function provides comprehensive Jest configuration:
**/?(*.)(spec|test|e2e).(j|t)s?(x)// Setup files loaded before tests:
// 1. require.resolve('./shim.js') - Babel polyfill and requestAnimationFrame shim
// 2. require.resolve('./setupTests.js') - JSDOM and Enzyme configuration
// Test framework setup:
// setupTestFrameworkScriptFile: require.resolve('./jasmine.js') - Sets jasmine timeout to 20000ms
// Module name mapping:
// CSS/LESS/SASS/SCSS files are mocked with identity-obj-proxyUsers can provide additional Jest configuration by creating a jest.config.js file in their project root:
// jest.config.js
module.exports = {
// Custom configuration that will be merged with default umi-test config
testTimeout: 30000,
collectCoverageFrom: [
'src/**/*.{js,jsx,ts,tsx}',
'!src/**/*.d.ts',
],
// Additional Jest options...
};The test function returns a Promise that:
// Jest Configuration Generated:
{
rootDir: process.cwd(),
setupFiles: [
require.resolve('./shim.js'),
require.resolve('./setupTests.js')
],
transform: {
'\\.jsx?$': require.resolve('./transformers/jsTransformer'),
'\\.tsx?$': require.resolve('./transformers/tsTransformer')
},
testMatch: ['**/?(*.)(spec|test|e2e).(j|t)s?(x)'],
moduleFileExtensions: ['js', 'jsx', 'ts', 'tsx', 'json'],
setupTestFrameworkScriptFile: require.resolve('./jasmine'),
moduleNameMapper: {
'\\.(css|less|sass|scss)$': require.resolve('identity-obj-proxy'),
// Additional mappings from opts.moduleNameMapper
},
globals: {
'ts-jest': {
useBabelrc: true
}
}
// User jest.config.js is merged if present
}