UmiJS Jest Testing Utility
The `umi-test` package (version 1.9.7) is a Jest-based utility designed to streamline testing configurations for UmiJS applications, primarily targeting UmiJS versions prior to v4. It provided pre-configured test framework setups and transformers, abstracting much of the boilerplate associated with Jest setup in a UmiJS environment. However, this specific package version was last published approximately four years ago (as of May 2022). For modern UmiJS v4 projects, the recommended approach for testing involves using `@umijs/preset-jest` in conjunction with Jest and `@testing-library/react`, or leveraging the integrated testing features of `@umijs/max`. Therefore, `umi-test` (v1.9.7) is considered largely superseded and no longer actively maintained, with a very slow or absent release cadence. Its key differentiator was simplifying Jest integration for older UmiJS projects, a role now filled by official presets and the `max` package for current UmiJS versions.
Common errors
-
SyntaxError: Unexpected token import at ScriptTransformer._transformAndBuildScript
cause Jest's transformer (often Babel) encounters ES module syntax (e.g., `import`) in files it's not configured to transform, typically when TypeScript's `module` option is `es6` or `esnext` for the test compilation.fixIn your `jest.config.js`, ensure the `transform` and `transformIgnorePatterns` options are correctly set, or create a `tsconfig.test.json` with `"module": "commonjs"` and point Jest to it. UmiJS presets usually handle this by default. -
process.env.YOUR_VARIABLE is undefined
cause Environment variables configured for the UmiJS development server are not automatically available in the Jest test environment.fixAdd a `globals` section to your `jest.config.js` to define the required `process.env` variables for your test runs. For example: `module.exports = { globals: { 'process.env.PRODUCT_MODEL': 'test-model' } };`.
Warnings
- breaking The `umi-test` package (v1.9.7) is considered abandoned. For UmiJS v4 and newer, use `@umijs/preset-jest` or `@umijs/max` for Jest integration, as the framework's testing strategy has evolved significantly. Direct usage of `umi-test` with UmiJS v4 is not recommended and will likely lead to compatibility issues.
- gotcha If `tsconfig.json` has `module` set to `es6` or `esnext`, Jest might fail with a `SyntaxError: Unexpected token import` because Jest's default transformer (Babel) may not correctly process ES modules in `node_modules` or `src` without proper configuration.
- gotcha Environment variables (`process.env.VAR_NAME`) might appear `undefined` when running tests, especially if they are usually defined by the UmiJS build process (e.g., in `umi dev`). Test environments often require explicit variable definitions.
Install
-
npm install umi-test -
yarn add umi-test -
pnpm add umi-test
Imports
- render
const { render } = require('@testing-library/react');import { render } from '@testing-library/react'; - jest-dom
require('@testing-library/jest-dom');import '@testing-library/jest-dom';
- configUmiAlias
import { configUmiAlias } from '@umijs/preset-jest';import { createConfig, configUmiAlias } from 'umi/test';
Quickstart
import { render, screen } from '@testing-library/react';
import '@testing-library/jest-dom';
import React from 'react';
// Assuming you have a component like this in src/components/Greeting.tsx
const Greeting = ({ name = 'World' }) => <h1>Hello, {name}!</h1>;
describe('Greeting Component', () => {
test('renders default greeting', () => {
render(<Greeting />);
expect(screen.getByText('Hello, World!')).toBeInTheDocument();
});
test('renders greeting with a specific name', () => {
render(<Greeting name="Umi" />);
expect(screen.getByText('Hello, Umi!')).toBeInTheDocument();
});
test('renders a snapshot correctly', () => {
const { asFragment } = render(<Greeting name="Snapshot" />);
expect(asFragment()).toMatchSnapshot();
});
});
// To run this test, your package.json would typically have:
// {
// "scripts": {
// "test": "jest"
// },
// "jest": {
// "preset": "@umijs/preset-jest" // Or earlier, configured by umi-test
// }
// }