Nnodejs

nodejs/mocha-to-node-test-runner

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

transformationmigrationmochatest
Public
6 executions

Run locally

npx codemod @nodejs/mocha-to-node-test-runner

Mocha to Node.js Test Runner

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

Features

  • 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

Example 2: Async

diff

Example 3: Hooks

diff

Example 4: Done

diff

Example 5: Skipped

diff

Example 6: Dynamic

diff

Example 7: Timeouts

diff

Caveats

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

References

Ready to contribute?

Build your own codemod and share it with the community.