Using Jest

Modern.js integrates the testing capabilities of Jest by default.

First need to execute pnpm run new to enable "unit test/integration test" features:

? Please select the operation you want: Enable features
? Please select the feature name: Enable Unit Test / Integration Test

After executing the above command, the "test": "modern test" command will be added in package.json automatically.

After registering the @modern-js/plugin-testing plugin in modern.config.ts, you can use the testing features:

modern.config.ts
import { testingPlugin } from '@modern-js/plugin-testing';

export default defineConfig({
  plugins: [..., testingPlugin()],
});

Test file

Modern.js default recognized test file paths are: <rootDir>/src/**/*.test.[jt]s?(x) and <rootDir>/tests/**/*.test.[jt]s?(x).

If you need to customize the test directory, you can configure it with tools.jest.

Usage

Modern.js test support testing-library. API can be imported from @modern-js/runtime/testing.

import { render, screen } from '@modern-js/runtime/testing';

Other testing APIs supported by Modern.js can be referred to here.

transform

By default, Modern.js testing uses babel-jest for source code compilation. If you need to use ts-jest, you can configure it through testing.transform.