本页内容

将 Mocha 8.x 测试套件迁移到内置的 Node.js 测试运行器(node:test,在 Node.js 22.x 和 24.x 中可用)。它会为文件中使用到的全局变量(describe、it、before、after、beforeEach、afterEach)添加所需的 node:test 导入,将 done 回调转换为 (t, done) 签名,把 this.skip() 重写为 t.skip()、把 this.timeout(N) 重写为 { timeout: N } 选项,并保留原有的函数风格(绝不在 function() 和箭头函数之间转换)。同时支持 CommonJS 和 ESM 文件,迁移后还会从 package.json 中移除 mocha 和 @types/mocha 依赖。

运行此 codemod:

插入匹配的 require('node:test') 后,全局的 describe/it 用法仍然有效;像 describe.skip 这样的修饰符本身已经兼容。

 const assert = require('assert');
+const { describe, it } = require('node:test');

 describe('Array', function() {
   describe.skip('#indexOf()', function() {
     it('should return -1 when the value is not present', function() {
       const arr = [1, 2, 3];
       assert.strictEqual(arr.indexOf(4), -1);
     });
   });
 });

在 ESM 文件中,会插入 import 语句代替。

 import assert from 'assert';
+import { describe, it } from 'node:test';

 describe('Array', function() {
   describe.skip('#indexOf()', function() {
     it('should return -1 when the value is not present', function() {

只把文件中实际用到的钩子添加到导入列表中。

 const assert = require('assert');
 const fs = require('fs');
+const { describe, before, after, it } = require('node:test');

 describe('File System', () => {
   before(function() {
     fs.writeFileSync('test.txt', 'Hello, World!');
   });

   after(() => {
     fs.unlinkSync('test.txt');
   });

Mocha 将 done 作为回调的第一个参数传入;node:test 则先传入测试上下文,因此 (done) 会变成 (t, done)。

 const assert = require('assert');
+const { describe, it } = require('node:test');

 describe('Callback Test', function() {
-  it('should call done when complete', function(done) {
+  it('should call done when complete', function(t, done) {
     setTimeout(() => {
       assert.strictEqual(1 + 1, 2);
       done();
     }, 100);
   });
 });

this.skip() 会变成 t.skip(),并按需在回调签名中添加测试上下文参数 t。

 const assert = require('assert');
+const { describe, it } = require('node:test');

 describe('Skipped Test', () => {
   it.skip('should not run this test', () => {
     assert.strictEqual(1 + 1, 3);
   });
-  it('should also be skipped', () => {
-    this.skip();
+  it('should also be skipped', (t) => {
+    t.skip();
     assert.strictEqual(1 + 1, 3);
   });

-  it('should also be skipped 2', (done) => {
-    this.skip();
+  it('should also be skipped 2', (t, done) => {
+    t.skip();
     assert.strictEqual(1 + 1, 3);
   });
 });

测试套件和测试上的 this.timeout(N) 调用会移入 { timeout: N } 选项参数。

 const assert = require('assert');
+const { describe, it } = require('node:test');

-describe('Timeout Test', function() {
-  this.timeout(500);
+describe('Timeout Test', { timeout: 500 }, function() {

-  it('should complete within 100ms', (done) => {
-    this.timeout(100);
+  it('should complete within 100ms', { timeout: 100 }, (t, done) => {
     setTimeout(done, 500); // 这会失败
   });

-  it('should complete within 200ms', function(done) {
-    this.timeout(200);
+  it('should complete within 200ms', { timeout: 200 }, function(t, done) {
     setTimeout(done, 100); // 这会通过
   });
 });
  • 转换后,codemod 会检测你的包管理器,并从 package.json 中移除 mocha 和 @types/mocha 依赖。
  • node:test 不支持 Mocha 的 retry 选项,因此依赖它的测试需要单独处理。