-
-
Notifications
You must be signed in to change notification settings - Fork 33
feat(mocha-to-node-test-runner): migrate mocha@8.x to node:test (v22, v24+)
#313
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
7dc0c74
11d3fd6
99e0ab4
ef337d5
759291a
7262ab1
ebdc996
be89c04
ead6abc
cf520d2
4377e8d
003b2df
1192ded
e59e09b
5a35723
d7ebcff
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,171 @@ | ||||||
| # Mocha to Node.js Test Runner | ||||||
|
|
||||||
| This recipe migrate Mocha v8 tests to Node.js test runner (v22, v24+) | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
|
||||||
| ## Features | ||||||
Xstoudi marked this conversation as resolved.
Show resolved
Hide 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 | ||||||
Xstoudi marked this conversation as resolved.
Show resolved
Hide 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 on lines
+24
to
+31
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: spaces (instead of tabs) please in markdown codeblocks because most viewers render tabs enormous 😭 |
||||||
| ``` | ||||||
|
|
||||||
| ### Example 2: Async | ||||||
|
|
||||||
| ```diff | ||||||
Xstoudi marked this conversation as resolved.
Show resolved
Hide 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 | ||||||
Xstoudi marked this conversation as resolved.
Show resolved
Hide 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 | ||||||
Xstoudi marked this conversation as resolved.
Show resolved
Hide 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 | ||||||
Xstoudi marked this conversation as resolved.
Show resolved
Hide 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 | ||||||
Xstoudi marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| ``` | ||||||
| const assert = require('assert'); | ||||||
| +const { describe, it } = require('node:test'); | ||||||
| describe('Dynamic Tests', () => { | ||||||
| const tests = [1, 2, 3]; | ||||||
|
|
||||||
| tests.forEach((test) => { | ||||||
Xstoudi marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| it(`should handle test ${test}`, () => { | ||||||
| assert.strictEqual(test % 2, 0); | ||||||
| }); | ||||||
| }); | ||||||
| }); | ||||||
| ``` | ||||||
|
|
||||||
| ### Example 7: Timeouts | ||||||
|
|
||||||
| ```diff | ||||||
Xstoudi marked this conversation as resolved.
Show resolved
Hide 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/) | ||||||
| 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 v8 tests to Node.js test runner (v22, v24+) | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| author: Xavier Stouder | ||||||
| license: MIT | ||||||
| workflow: workflow.yaml | ||||||
| category: migration | ||||||
|
|
||||||
| targets: | ||||||
| languages: | ||||||
| - javascript | ||||||
| - typescript | ||||||
|
|
||||||
| keywords: | ||||||
| - transformation | ||||||
| - migration | ||||||
| - mocha | ||||||
| - test | ||||||
|
|
||||||
| registry: | ||||||
| access: public | ||||||
| visibility: public | ||||||
| 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 v8 tests to Node.js test runner (v22, v24+)", | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| "type": "module", | ||||||
| "scripts": { | ||||||
| "test": "npx codemod jssg test -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": "*" | ||||||
| } | ||||||
| } | ||||||
| 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; |
Uh oh!
There was an error while loading. Please reload this page.