feat(mocha-to-node-test-runner): migrate mocha@8.x to node:test (v22, v24+)#313
feat(mocha-to-node-test-runner): migrate mocha@8.x to node:test (v22, v24+)#313Xstoudi wants to merge 16 commits intonodejs:mainfrom
node:test (v22, v24+)#313Conversation
AugustinMauroy
left a comment
There was a problem hiding this comment.
Oh nice that really cool !
Few points that can be improved:
- md format on readme
- little bit of JSdoc
- small missing unit test
Co-authored-by: Augustin Mauroy <97875033+AugustinMauroy@users.noreply.github.com>
|
I wrote test for is-esm, let me know if you see something is missing. I applied your recommendations then reverted two of them:
Let me now if there is anything else. |
| it('should return -1 when the value is not present', function() { | ||
| const arr = [1, 2, 3]; | ||
| assert.strictEqual(arr.indexOf(4), -1); | ||
| }); |
There was a problem hiding this comment.
maybe add an other it that use arrow function as call back
and a fonction as arg like
function myBasicTest = () => {
assert(true)
}The second case will be can super complicated to migrate.
in a first time let's just have test that proof that's ins't handled and document it.
In second time (follow up pr) having systems that handle this case. The logic to catch a refercen is already furnished by the codemod context https://docs.codemod.com/jssg/semantic-analysis
|
Let me know if anything more is required. |
AugustinMauroy
left a comment
There was a problem hiding this comment.
LGMT ! for the code transformation part missing the removing dep you should take code from chalk-to-styletext.
|
Added that, let me know if something is missing. |
AugustinMauroy
left a comment
There was a problem hiding this comment.
For me it's good ! we need to wait a second approval to land !
|
F*ck what happened here. !Windows! cc @nodejs/userland-migrations any idea |
|
hey 👋 @Xstoudi |
Co-authored-by: Bruno Rodrigues <swe@brunocroh.com>
AugustinMauroy
left a comment
There was a problem hiding this comment.
LGTM ! I stil don't get why windows give this error
Maybe use lower comparison level on testing https://docs.codemod.com/jssg/testing#strictness-levels
|
The windows testing is failing with: Where it looks like the entire contents of the expected file isn't there. @mohebifar have you seen anything like this before? |
node:test (v22, v24+)
node:test (v22, v24+)node:test (v22, v24+)
node:test (v22, v24+)node:test (v22, v24+)
JakobJingleheimer
left a comment
There was a problem hiding this comment.
Thanks for this! 🙌
A few nits here and there.
For the test files, could the names please be more descriptive (eg 3_hooks is not so illuminating).
Also, there are 2 options for organising test files. We do have a bit of a mix, but option 1 is far superior to the "junk-drawer" option 2. Would you mind adjusting these to option 1? If you don't want to bother with it, that's fine—but would you mind if one of us made the adjustment within this PR?
I haven't reviewed the test cases yet because it's so cumbersome with option 2.
| const globalIdentifiers = ['describe']; | ||
|
|
||
| const usedGlobalIdentifiers = globalIdentifiers.filter((globalIdentifier) => | ||
| ['', '.skip', '.only'] |
There was a problem hiding this comment.
nit: this is constant / static, so better to move to the root of this module (in which case, please make the name screaming snake_case: USED_GLOBAL_IDENTIFIERS
| const usedMochaGlobals = [ | ||
| ...new Set( | ||
| mochaGlobalsNodes.map( | ||
| (mochaGlobalsNode) => | ||
| mochaGlobalsNode.getMatch('MOCHA_GLOBAL_FN').text().split('.')[0], | ||
| ), | ||
| ), | ||
| ]; | ||
|
|
||
| // if mocha isn't found, don't try to apply changes | ||
| if (usedMochaGlobals.length === 0) return []; |
There was a problem hiding this comment.
nit: check the size of the Set (and abort) before converting it to an array to avoid the expense (micro-op, but no need to leave money on the table).
| if (thisKeyword) { | ||
| edits.push(thisKeyword.replace('t')); | ||
| } |
There was a problem hiding this comment.
nit: some places inline this but others don't; would be nice to keep it consistent (I personally prefer concise)
| if (thisKeyword) { | |
| edits.push(thisKeyword.replace('t')); | |
| } | |
| if (thisKeyword) edits.push(thisKeyword.replace('t')); |
| if (!fn) { | ||
| return edits; | ||
| } |
There was a problem hiding this comment.
same nit
| if (!fn) { | |
| return edits; | |
| } | |
| if (!fn) return edits; |
| capabilities: | ||
| - fs | ||
| - child_process | ||
| description: Migrate Mocha v8 tests to Node.js test runner (v22, v24+) |
There was a problem hiding this comment.
| description: Migrate Mocha v8 tests to Node.js test runner (v22, v24+) | |
| description: Migrate Mocha 8.x tests to Node.js (22.x, 24.x) test runner |
| const isCjsFile = filename.endsWith('.cjs') || filename.endsWith('.cts'); | ||
| const isMjsFile = filename.endsWith('.mjs') || filename.endsWith('.mts'); | ||
| if (isMjsFile) { | ||
| return true; | ||
| } | ||
| if (isCjsFile) { | ||
| return false; | ||
| } |
There was a problem hiding this comment.
nit for 2 reasons:
- those
consts are not used below (and it's pretty clear what they're holding) so no need to keep them in memory after they're read the once - micro-op: you don't need to check
isCjsFilewhenisMjsFile
| const isCjsFile = filename.endsWith('.cjs') || filename.endsWith('.cts'); | |
| const isMjsFile = filename.endsWith('.mjs') || filename.endsWith('.mts'); | |
| if (isMjsFile) { | |
| return true; | |
| } | |
| if (isCjsFile) { | |
| return false; | |
| } | |
| if (filename.endsWith('.mjs') || filename.endsWith('.mts')) return true; | |
| if (filename.endsWith('.cjs') || filename.endsWith('.cts')) return false; |
| const rootNode = root.root(); | ||
| const usingImport = rootNode.find({ |
There was a problem hiding this comment.
nit (these are not coupled)
| const rootNode = root.root(); | |
| const usingImport = rootNode.find({ | |
| const rootNode = root.root(); | |
| const usingImport = rootNode.find({ |
| if (usingImport) { | ||
| return true; | ||
| } |
There was a problem hiding this comment.
nit for consistency
| if (usingImport) { | |
| return true; | |
| } | |
| if (usingImport) return true; |
| if (usingRequire) { | ||
| return false; | ||
| } |
There was a problem hiding this comment.
same nit
| if (usingRequire) { | |
| return false; | |
| } | |
| if (usingRequire) return false; |
| return false; | ||
| } | ||
|
|
||
| const packageJsonPath = join(process.cwd(), 'package.json'); |
There was a problem hiding this comment.
@AugustinMauroy what was the verdict on node:module.findPackageJSON()?
Should fix #103