Skip to content

Conversation

@eliOcs
Copy link

@eliOcs eliOcs commented Nov 8, 2025

Transform function that converts Mocha tests to Node.js test runner.

Handles:

  1. Adding node:test imports/requires
  2. Converting done callbacks to (t, done) signature
  3. Converting this.skip() to t.skip()
  4. Converting this.timeout() to { timeout: N } options

Related to #103

@eliOcs eliOcs marked this pull request as draft November 8, 2025 08:17
Copy link
Member

@AugustinMauroy AugustinMauroy left a comment

Choose a reason for hiding this comment

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

don't forget to run npm ci to add this new workspace to the lock file

Mention on readme that is design for mocha v8 & node v22, v24 is a super point.

Also I saw that now your tests keep function keyword for callback. maybe add example with arrow function

Instead good first commit. And wow, you really put your foot in it for a first contribution.

Copy link
Member

Choose a reason for hiding this comment

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

we have some utility for ast-grep, you may need them you have doc here https://github.com/nodejs/userland-migrations/tree/main/utils

Also genre note about the codemod too much regex which is unsafe and to complex. To avoid that you have to use ast-grep ast query api that look like that

const statement = root.findAll({
		rule: {
			pattern: `${base}.propriety`,
			not: {
				inside: {
					kind: "assignment_expression",
					has: {
						kind: "member_expression",
						pattern: `${base}.fips`,
					},
				},
			},
		},
	});

To have representation of ast / testing query you can use ast-grep playground https://ast-grep.github.io/playground.html

@AugustinMauroy AugustinMauroy linked an issue Nov 8, 2025 that may be closed by this pull request
@eliOcs eliOcs changed the title mocha-to-node-test-runner codemod WIP feat(mocha-to-node-test-runner): add new Mocha v8 to Node v22, v24 test runner migration codemod Nov 8, 2025
@eliOcs
Copy link
Author

eliOcs commented Nov 8, 2025

Thanks for your review and patience @AugustinMauroy and kind words, yep it is my first open source contribution 😅. I will continue working to address your comments.

@eliOcs
Copy link
Author

eliOcs commented Nov 10, 2025

@AugustinMauroy I'm having a lot of trouble trying to setup the codemod. I'm used to jscodeshift in which you modify the AST but in this type of codemod you are generating location based edits and each of your edits can conflict with each other which makes this kind of transform very brittle.
Is there any example out there of a codemods which deals with nested edits I can learn from?

@AugustinMauroy
Copy link
Member

@AugustinMauroy I'm having a lot of trouble trying to setup the codemod. I'm used to jscodeshift in which you modify the AST but in this type of codemod you are generating location based edits and each of your edits can conflict with each other which makes this kind of transform very brittle. Is there any example out there of a codemods which deals with nested edits I can learn from?

Wow I'm surprised to see that happened. For example we have the whole repo.
Also @alexbit-codemod can help you. he is CEO of codemod and previously at Meta so he can help yo

@alexbit-codemod
Copy link
Contributor

hey @eliOcs! thanks a lot for contributing to node codemods.

i’m sharing a few pointers that might help. if you have more questions, feel free to join the Codemod community
and ask there. folks in the community could jump in and help.

  • jssg docs
  • get AI help building your jssg codemods by installing Codemod MCP in your favorite IDE or using Codemod Studio
  • im not quite sure what you mean by nested edits. can you explain what you mean with an example? also, have you checked out some of the existing codemods in this repo?

// ESM - add import statement
const importStatement = `import { ${testImports.join(', ')} } from "node:test";\n`;
// Insert after first import or at the beginning
const firstImportMatch = sourceCode.match(
Copy link
Member

Choose a reason for hiding this comment

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

you just have to catch import_statement kind in your ast. You always need to try to remove avoid string manipulation

// CJS
const requireStatement = `\n\nconst {\n ${testImports.join(',\n ')}\n} = require("node:test");\n`;
// Insert after first require or at the beginning
const firstRequireMatch = sourceCode.match(
Copy link
Member

Choose a reason for hiding this comment

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

for that you have to catch require call inside of variable_declaration or variable_declaration then use methods range on node and the obtain position.

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.

brainstorm: mocha-to-node-test-runner

3 participants