Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
7dc0c74
wip
Xstoudi Dec 21, 2025
11d3fd6
wip
Xstoudi Dec 21, 2025
99e0ab4
wip
Xstoudi Dec 22, 2025
ef337d5
wip
Xstoudi Dec 22, 2025
759291a
wip
Xstoudi Dec 22, 2025
7262ab1
Apply suggestions from code review
Xstoudi Dec 23, 2025
ebdc996
wip
Xstoudi Dec 23, 2025
be89c04
fix
Xstoudi Dec 26, 2025
ead6abc
fix
Xstoudi Dec 30, 2025
cf520d2
fix
Xstoudi Dec 30, 2025
4377e8d
Update recipes/mocha-to-node-test-runner/src/workflow.ts
Xstoudi Mar 11, 2026
003b2df
Merge branch 'nodejs:main' into feature/mocha-to-node-test-runner
Xstoudi Mar 11, 2026
1192ded
factorize
Xstoudi Mar 11, 2026
e59e09b
cover esm
Xstoudi Mar 11, 2026
5a35723
update remove dependencies step
Xstoudi Mar 11, 2026
d7ebcff
lint
Xstoudi Mar 11, 2026
d0aa97f
Apply suggestions from code review
Xstoudi Apr 1, 2026
ca630d2
move constant out of module
Xstoudi Apr 1, 2026
c1d9849
micro-op
Xstoudi Apr 1, 2026
b5f8b6e
consistency
Xstoudi Apr 1, 2026
075b7d7
consistency
Xstoudi Apr 1, 2026
c9e269a
improve markdown
Xstoudi Apr 1, 2026
91d86f0
update utils
Xstoudi Apr 1, 2026
0368155
Merge branch 'main' into pr/313
AugustinMauroy Jul 2, 2026
25d51e7
Update package-lock.json
AugustinMauroy Jul 2, 2026
56f8598
fix
AugustinMauroy Jul 2, 2026
34301a2
Update package.json
AugustinMauroy Jul 2, 2026
199d039
Update package.json
AugustinMauroy Jul 2, 2026
8ce5fe8
re-organize tests
AugustinMauroy Jul 2, 2026
f867270
re-organize tests
AugustinMauroy Jul 2, 2026
e6ec628
test thing
AugustinMauroy Jul 2, 2026
80ae543
update
AugustinMauroy Jul 2, 2026
fddf9bf
cache range
AugustinMauroy Jul 2, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion recipes/fs-access-mode-constants/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Handle DEP0176 via transforming imports of `fs.F_OK`, `fs.R_OK`, `fs.W_OK`, `fs.X_OK` from the root `fs` module to `fs.constants`.",
"type": "module",
"scripts": {
"test": "npx codemod jssg test -l typescript ./src/workflow.ts ./tests"
"test": "npx codemod jssg test --allow-fs -l typescript ./src/workflow.ts ./tests"
},
"repository": {
"type": "git",
Expand Down
165 changes: 165 additions & 0 deletions recipes/mocha-to-node-test-runner/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
# Mocha to Node.js Test Runner
Comment thread
Xstoudi marked this conversation as resolved.

This migrates Mocha 8.x tests to Node.js (22.x, 24.x) test runner

## Features
Comment thread
Xstoudi marked this conversation as resolved.

- Automatically adds `node:test` imports/requires
- Converts global `describe`, `it`, and hooks to imported versions
- Transforms `done` callbacks to `(t, done)` signature
- Converts `this.skip()` to `t.skip()`
- Converts `this.timeout(N)` to `{ timeout: N }` options
- Preserves function styles (doesn't convert between `function()` and arrow functions)
- Supports both CommonJS and ESM

## Examples

### Example 1: Basic

```diff
Comment thread
Xstoudi marked this conversation as resolved.
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);
});
});
});
Comment thread
Xstoudi marked this conversation as resolved.
```

### Example 2: Async

```diff
Comment thread
Xstoudi marked this conversation as resolved.
const assert = require('assert');
+const { describe, it } = require('node:test');
describe('Async Test', function() {
-it('should complete after a delay', async function(done) {
+it('should complete after a delay', async function(t, done) {
const result = await new Promise(resolve => setTimeout(() => resolve(42), 100));
assert.strictEqual(result, 42);
});
});
```

### Example 3: Hooks

```diff
Comment thread
Xstoudi marked this conversation as resolved.
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');
});

it('should read the file', () => {
const content = fs.readFileSync('test.txt', 'utf8');
assert.strictEqual(content, 'Hello, World!');
});
});
```

### Example 4: Done

```diff
Comment thread
Xstoudi marked this conversation as resolved.
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);
});
})
```

### Example 5: Skipped

```diff
Comment thread
Xstoudi marked this conversation as resolved.
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);
});

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

### Example 6: Dynamic

```diff
Comment thread
Xstoudi marked this conversation as resolved.
const assert = require('assert');
+const { describe, it } = require('node:test');
describe('Dynamic Tests', () => {
const tests = [1, 2, 3];

tests.forEach((test) => {
it(`should handle test ${test}`, () => {
assert.strictEqual(test % 2, 0);
});
});
});
```

### Example 7: Timeouts

```diff
Comment thread
Xstoudi marked this conversation as resolved.
const assert = require('assert');
-describe('Timeout Test', function() {
- this.timeout(500);
+const { describe, it } = require('node:test');
+describe('Timeout Test', { timeout: 500 }, function() {
+
+
+ it('should complete within 100ms', { timeout: 100 }, (t, done) => {

- it('should complete within 100ms', (done) => {
- this.timeout(100);
setTimeout(done, 500); // This will fail
});

- it('should complete within 200ms', function(done) {
- this.timeout(200);
+ it('should complete within 200ms', { timeout: 200 }, function(t, done) {
+
setTimeout(done, 100); // This will pass
});
});
```

## Caveats

* `node:test` doesn't support the `retry` option that Mocha has, so any tests using that will need to be handled separately.

## References
- [Node Test Runner](https://nodejs.org/api/test.html)
- [Mocha](https://mochajs.org/)
26 changes: 26 additions & 0 deletions recipes/mocha-to-node-test-runner/codemod.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
schema_version: "1.0"
name: "@nodejs/mocha-to-node-test-runner"
version: 1.0.0
capabilities:
- fs
- child_process
description: Migrate Mocha 8.x tests to Node.js (22.x, 24.x) test runner
author: Xavier Stouder
license: MIT
workflow: workflow.yaml
category: migration

targets:
languages:
- javascript
- typescript

keywords:
- transformation
- migration
- mocha
- test

registry:
access: public
visibility: public
24 changes: 24 additions & 0 deletions recipes/mocha-to-node-test-runner/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "@nodejs/mocha-to-node-test-runner",
"version": "1.0.0",
"description": "Migrate Mocha 8.x tests to Node.js (22.x, 24.x) test runner",
"type": "module",
"scripts": {
"test": "npx codemod jssg test --strictness cst -l typescript ./src/workflow.ts"
},
"repository": {
"type": "git",
"url": "git+https://github.com/nodejs/userland-migrations.git",
"directory": "recipes/mocha-to-node-test-runner",
"bugs": "https://github.com/nodejs/userland-migrations/issues"
},
"author": "Richie McColl",
"license": "MIT",
"homepage": "https://github.com/nodejs/userland-migrations/blob/main/recipes/mocha-to-node-test-runner/README.md",
"devDependencies": {
"@codemod.com/jssg-types": "^1.3.1"
},
"dependencies": {
"@nodejs/codemod-utils": "*"
}
}
13 changes: 13 additions & 0 deletions recipes/mocha-to-node-test-runner/src/remove-dependencies.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import type { Transform } from '@codemod.com/jssg-types/main';
import type Json from '@codemod.com/jssg-types/langs/json';
import removeDependencies from '@nodejs/codemod-utils/remove-dependencies';

const transform: Transform<Json> = async (root) => {
return removeDependencies(['mocha', '@types/mocha'], {
packageJsonPath: root.filename(),
runInstall: false,
persistFileWrite: false,
});
};

export default transform;
Loading
Loading