{"id":15008,"library":"umi-test","title":"UmiJS Jest Testing Utility","description":"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.","status":"abandoned","version":"1.9.7","language":"javascript","source_language":"en","source_url":"ssh://git@github.com/umijs/umi#master","tags":["javascript"],"install":[{"cmd":"npm install umi-test","lang":"bash","label":"npm"},{"cmd":"yarn add umi-test","lang":"bash","label":"yarn"},{"cmd":"pnpm add umi-test","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Core testing framework that umi-test is built upon.","package":"jest","optional":false},{"reason":"Commonly used for React component testing within UmiJS projects, facilitated by umi-test's configuration.","package":"@testing-library/react","optional":false},{"reason":"Provides custom Jest matchers for testing the DOM state, frequently used with React Testing Library in UmiJS projects.","package":"@testing-library/jest-dom","optional":false}],"imports":[{"note":"Commonly imported for rendering React components in UmiJS tests. While `umi-test` sets up the environment, the actual testing utilities come from `@testing-library/react`. ESM syntax is standard for test files.","wrong":"const { render } = require('@testing-library/react');","symbol":"render","correct":"import { render } from '@testing-library/react';"},{"note":"Imports extend Jest's `expect` with DOM-specific matchers. This is a side-effect import and is crucial for ergonomic DOM assertions. ESM syntax is preferred in modern test setups.","wrong":"require('@testing-library/jest-dom');","symbol":"jest-dom","correct":"import '@testing-library/jest-dom';"},{"note":"For Umi v4, the `umi/test` module provides utilities for Jest configuration, including alias resolution. This is not directly from `umi-test` but reflects the modern UmiJS testing approach which superseded `umi-test`.","wrong":"import { configUmiAlias } from '@umijs/preset-jest';","symbol":"configUmiAlias","correct":"import { createConfig, configUmiAlias } from 'umi/test';"}],"quickstart":{"code":"import { render, screen } from '@testing-library/react';\nimport '@testing-library/jest-dom';\nimport React from 'react';\n\n// Assuming you have a component like this in src/components/Greeting.tsx\nconst Greeting = ({ name = 'World' }) => <h1>Hello, {name}!</h1>;\n\ndescribe('Greeting Component', () => {\n  test('renders default greeting', () => {\n    render(<Greeting />);\n    expect(screen.getByText('Hello, World!')).toBeInTheDocument();\n  });\n\n  test('renders greeting with a specific name', () => {\n    render(<Greeting name=\"Umi\" />);\n    expect(screen.getByText('Hello, Umi!')).toBeInTheDocument();\n  });\n\n  test('renders a snapshot correctly', () => {\n    const { asFragment } = render(<Greeting name=\"Snapshot\" />);\n    expect(asFragment()).toMatchSnapshot();\n  });\n});\n\n// To run this test, your package.json would typically have:\n// {\n//   \"scripts\": {\n//     \"test\": \"jest\"\n//   },\n//   \"jest\": {\n//     \"preset\": \"@umijs/preset-jest\" // Or earlier, configured by umi-test\n//   }\n// }","lang":"typescript","description":"Demonstrates a basic unit test for a React component within an UmiJS project, utilizing Jest and React Testing Library, which `umi-test` (or `@umijs/preset-jest` for Umi v4) would configure."},"warnings":[{"fix":"Migrate testing setup to use `@umijs/preset-jest` in your `jest.config.js` or leverage the built-in testing features of `@umijs/max`. Example: `npm install --save-dev jest @umijs/preset-jest @testing-library/react @testing-library/jest-dom`.","message":"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.","severity":"breaking","affected_versions":">=1.9.7 (for UmiJS v4+ projects)"},{"fix":"Ensure your `jest.config.js` or `tsconfig.json` (specifically for test environment) correctly configures module resolution for Jest. A common fix is to set `module` to `commonjs` in a dedicated `tsconfig.test.json` that Jest uses, or to configure `transformIgnorePatterns` in `jest.config.js` to include relevant modules for transformation. For UmiJS, `@umijs/preset-jest` usually handles this.","message":"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.","severity":"gotcha","affected_versions":">=1.0.0 (all versions using Jest with TypeScript)"},{"fix":"Define necessary environment variables in your `jest.config.js` using the `globals` option, or ensure they are set in the shell environment before running tests. For example: `globals: { 'process.env.VAR_NAME': 'test_value' }`.","message":"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.","severity":"gotcha","affected_versions":">=1.0.0 (all versions)"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"In 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.","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.","error":"SyntaxError: Unexpected token import at ScriptTransformer._transformAndBuildScript"},{"fix":"Add 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' } };`.","cause":"Environment variables configured for the UmiJS development server are not automatically available in the Jest test environment.","error":"process.env.YOUR_VARIABLE is undefined"}],"ecosystem":"npm"}