Skip to content

Replace ts-jest with @swc/jest for TypeScript test transforms - #313

Merged
hzhangxyz merged 3 commits into
mainfrom
copilot/swc-jest-ts-jest
Apr 6, 2026
Merged

Replace ts-jest with @swc/jest for TypeScript test transforms#313
hzhangxyz merged 3 commits into
mainfrom
copilot/swc-jest-ts-jest

Conversation

Copilot AI commented Apr 6, 2026

Copy link
Copy Markdown
Contributor
  • Update root jest.config.mjs: replace ts-jest transform with @swc/jest
  • Update egg/jest.config.mjs: replace ts-jest transform with @swc/jest
  • Update root package.json: remove ts-jest, add @swc/core and @swc/jest devDependencies
  • Update egg/package.json: remove ts-jest, add @swc/core and @swc/jest devDependencies
  • Run npm install in root and egg/ to update package-lock.json
  • Fix CI failure: add SWC options (jsc.parser.syntax: "typescript", jsc.target: "es2022", module.type: "es6") to both jest configs so @swc/jest uses the TypeScript parser and emits ESM
  • Note: bnf/jest.config.mjs has no ts transform, no changes needed there

Copilot AI linked an issue Apr 6, 2026 that may be closed by this pull request
Copilot AI changed the title [WIP] 使用 swc jest 取代 ts-jest Replace ts-jest with @swc/jest for TypeScript test transforms Apr 6, 2026
Copilot AI requested a review from hzhangxyz April 6, 2026 12:32
@hzhangxyz
hzhangxyz marked this pull request as ready for review April 6, 2026 15:06
Copilot AI review requested due to automatic review settings April 6, 2026 15:06

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR replaces ts-jest with @swc/jest for transforming TypeScript during Jest runs to avoid tsc-triggered failures (notably TS5107 on deprecated moduleResolution modes) while keeping the Jest setup largely unchanged.

Changes:

  • Swaps Jest’s TypeScript transform from ts-jest to @swc/jest (root + egg/).
  • Updates devDependencies to remove ts-jest and add @swc/core + @swc/jest (root + egg/), with corresponding lockfile updates.

Reviewed changes

Copilot reviewed 4 out of 6 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
package.json Replaces ts-jest devDependency with @swc/core and @swc/jest.
package-lock.json Lockfile updates reflecting removal of ts-jest and addition of SWC packages.
jest.config.mjs Switches transform to @swc/jest.
egg/package.json Mirrors root dependency change for the egg/ package.
egg/package-lock.json Mirrors root lockfile changes for egg/.
egg/jest.config.mjs Mirrors root Jest transform change for egg/.
Files not reviewed (1)
  • egg/package-lock.json: Language not supported

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread jest.config.mjs Outdated
extensionsToTreatAsEsm: [".mts"],
transform: {
"^.+\\.m?tsx?$": ["ts-jest", {}],
"^.+\\.m?tsx?$": ["@swc/jest"],

Copilot AI Apr 6, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@swc/jest defaults to emitting CommonJS. Since this repo is ESM (package.json has type: "module") and Jest treats .mts as ESM (extensionsToTreatAsEsm: [".mts"]), transforming atsds/index.mts (which uses top-level await) to CommonJS is likely to break test execution. Configure the SWC transform to emit ESM (module type es6/esnext) and target a runtime that supports top-level await (e.g., es2022/esnext), either by passing options to the transform entry or by adding a project .swcrc used by Jest.

Suggested change
"^.+\\.m?tsx?$": ["@swc/jest"],
"^.+\\.m?tsx?$": [
"@swc/jest",
{
module: {
type: "es6",
},
jsc: {
target: "es2022",
},
},
],

Copilot uses AI. Check for mistakes.
Comment thread egg/jest.config.mjs Outdated
extensionsToTreatAsEsm: [".mts"],
transform: {
"^.+\\.m?tsx?$": ["ts-jest", {}],
"^.+\\.m?tsx?$": ["@swc/jest"],

Copilot AI Apr 6, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same concern as root config: with type: "module" and .mts treated as ESM, @swc/jest needs to be configured to output ESM (and a target that supports top-level await) or Jest may fail when importing .mts sources during tests.

Suggested change
"^.+\\.m?tsx?$": ["@swc/jest"],
"^.+\\.m?tsx?$": ["@swc/jest", {
jsc: {
parser: {
syntax: "typescript",
tsx: true,
},
target: "es2022",
},
module: {
type: "es6",
},
}],

Copilot uses AI. Check for mistakes.

@hzhangxyz hzhangxyz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

github action 报错了:

cross-env NODE_OPTIONS='$NODE_OPTIONS --experimental-vm-modules' jest --config=jest.config.mjs

(node:4308) ExperimentalWarning: VM Modules is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
FAIL tests/test_chain.mjs
  ● Test suite failed to run

      x Expected ',', got '*'
       ,-[/home/runner/work/ds/ds/atsds/index.mts:7:1]
     4 |  */
     5 | 
     6 | import create_ds from "./ds.mjs";
     7 | import type * as dst from "./ds.d.mts";
       :             ^
     8 | 
     9 | const ds: dst.EmbindModule = await create_ds();
       `----


    Caused by:
        Syntax Error

      ,-[/home/runner/work/ds/ds/atsds/index.mts:7:1]
       4 |  */
       5 | 
       6 | import create_ds from "./ds.mjs";
       7 | import type * as dst from "./ds.d.mts";
         :             ^
       8 | 
       9 | const ds: dst.EmbindModule = await create_ds();
         `----
      Caused by:
          Syntax Error

(node:4309) ExperimentalWarning: VM Modules is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
FAIL tests/test_rule.mjs
  ● Test suite failed to run

      x Expected ',', got '*'
       ,-[/home/runner/work/ds/ds/atsds/index.mts:7:1]
     4 |  */
     5 | 
     6 | import create_ds from "./ds.mjs";
     7 | import type * as dst from "./ds.d.mts";
       :             ^
     8 | 
     9 | const ds: dst.EmbindModule = await create_ds();
       `----


    Caused by:
        Syntax Error

      ,-[/home/runner/work/ds/ds/atsds/index.mts:7:1]
       4 |  */
       5 | 
       6 | import create_ds from "./ds.mjs";
       7 | import type * as dst from "./ds.d.mts";
         :             ^
       8 | 
       9 | const ds: dst.EmbindModule = await create_ds();
         `----
      Caused by:
          Syntax Error

(node:4310) ExperimentalWarning: VM Modules is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
FAIL tests/test_term.mjs
  ● Test suite failed to run

      x Expected ',', got '*'
       ,-[/home/runner/work/ds/ds/atsds/index.mts:7:1]
     4 |  */
     5 | 
     6 | import create_ds from "./ds.mjs";
     7 | import type * as dst from "./ds.d.mts";
       :             ^
     8 | 
     9 | const ds: dst.EmbindModule = await create_ds();
       `----


    Caused by:
        Syntax Error

      ,-[/home/runner/work/ds/ds/atsds/index.mts:7:1]
       4 |  */
       5 | 
       6 | import create_ds from "./ds.mjs";
       7 | import type * as dst from "./ds.d.mts";
         :             ^
       8 | 
       9 | const ds: dst.EmbindModule = await create_ds();
         `----
      Caused by:
          Syntax Error

FAIL tests/test_search.mjs
  ● Test suite failed to run

      x Expected ',', got '*'
       ,-[/home/runner/work/ds/ds/atsds/index.mts:7:1]
     4 |  */
     5 | 
     6 | import create_ds from "./ds.mjs";
     7 | import type * as dst from "./ds.d.mts";
       :             ^
     8 | 
     9 | const ds: dst.EmbindModule = await create_ds();
       `----


    Caused by:
        Syntax Error

      ,-[/home/runner/work/ds/ds/atsds/index.mts:7:1]
       4 |  */
       5 | 
       6 | import create_ds from "./ds.mjs";
       7 | import type * as dst from "./ds.d.mts";
         :             ^
       8 | 
       9 | const ds: dst.EmbindModule = await create_ds();
         `----
      Caused by:
          Syntax Error

FAIL tests/test_variable.mjs
  ● Test suite failed to run

      x Expected ',', got '*'
       ,-[/home/runner/work/ds/ds/atsds/index.mts:7:1]
     4 |  */
     5 | 
     6 | import create_ds from "./ds.mjs";
     7 | import type * as dst from "./ds.d.mts";
       :             ^
     8 | 
     9 | const ds: dst.EmbindModule = await create_ds();
       `----


    Caused by:
        Syntax Error

      ,-[/home/runner/work/ds/ds/atsds/index.mts:7:1]
       4 |  */
       5 | 
       6 | import create_ds from "./ds.mjs";
       7 | import type * as dst from "./ds.d.mts";
         :             ^
       8 | 
       9 | const ds: dst.EmbindModule = await create_ds();
         `----
      Caused by:
          Syntax Error

FAIL tests/test_list.mjs
  ● Test suite failed to run

      x Expected ',', got '*'
       ,-[/home/runner/work/ds/ds/atsds/index.mts:7:1]
     4 |  */
     5 | 
     6 | import create_ds from "./ds.mjs";
     7 | import type * as dst from "./ds.d.mts";
       :             ^
     8 | 
     9 | const ds: dst.EmbindModule = await create_ds();
       `----


    Caused by:
        Syntax Error

      ,-[/home/runner/work/ds/ds/atsds/index.mts:7:1]
       4 |  */
       5 | 
       6 | import create_ds from "./ds.mjs";
       7 | import type * as dst from "./ds.d.mts";
         :             ^
       8 | 
       9 | const ds: dst.EmbindModule = await create_ds();
         `----
      Caused by:
          Syntax Error

FAIL tests/test_item.mjs
  ● Test suite failed to run

      x Expected ',', got '*'
       ,-[/home/runner/work/ds/ds/atsds/index.mts:7:1]
     4 |  */
     5 | 
     6 | import create_ds from "./ds.mjs";
     7 | import type * as dst from "./ds.d.mts";
       :             ^
     8 | 
     9 | const ds: dst.EmbindModule = await create_ds();
       `----


    Caused by:
        Syntax Error

      ,-[/home/runner/work/ds/ds/atsds/index.mts:7:1]
       4 |  */
       5 | 
       6 | import create_ds from "./ds.mjs";
       7 | import type * as dst from "./ds.d.mts";
         :             ^
       8 | 
       9 | const ds: dst.EmbindModule = await create_ds();
         `----
      Caused by:
          Syntax Error

FAIL tests/test_string.mjs
  ● Test suite failed to run

      x Expected ',', got '*'
       ,-[/home/runner/work/ds/ds/atsds/index.mts:7:1]
     4 |  */
     5 | 
     6 | import create_ds from "./ds.mjs";
     7 | import type * as dst from "./ds.d.mts";
       :             ^
     8 | 
     9 | const ds: dst.EmbindModule = await create_ds();
       `----


    Caused by:
        Syntax Error

      ,-[/home/runner/work/ds/ds/atsds/index.mts:7:1]
       4 |  */
       5 | 
       6 | import create_ds from "./ds.mjs";
       7 | import type * as dst from "./ds.d.mts";
         :             ^
       8 | 
       9 | const ds: dst.EmbindModule = await create_ds();
         `----
      Caused by:
          Syntax Error

----------|---------|----------|---------|---------|-------------------
File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
----------|---------|----------|---------|---------|-------------------
All files |       0 |        0 |       0 |       0 |                   
----------|---------|----------|---------|---------|-------------------
Test Suites: 8 failed, 8 total
Tests:       0 total
Snapshots:   0 total
Time:        0.697 s
Ran all test suites.
ERROR: "test" exited with 1.
Error: Process completed with exit code 1.

Copilot AI commented Apr 6, 2026

Copy link
Copy Markdown
Contributor Author

github action 报错了:

Fixed in b522d17. Both jest.config.mjs and egg/jest.config.mjs now pass explicit SWC options to @swc/jest:

  • jsc.parser.syntax: "typescript" — enables the TypeScript parser so import type * is recognized
  • jsc.target: "es2022" — supports top-level await
  • module.type: "es6" — emits ESM, matching the type: "module" package config

Copilot AI requested a review from hzhangxyz April 6, 2026 15:23
@hzhangxyz
hzhangxyz merged commit 82ec5a0 into main Apr 6, 2026
86 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

使用 swc jest而不是ts-jest

3 participants