Discussion: allow extension to function when node:test is wrapped by a convenience function#63
Discussion: allow extension to function when node:test is wrapped by a convenience function#63skabbes wants to merge 1 commit intoconnor4312:mainfrom
node:test is wrapped by a convenience function#63Conversation
| traverse(ast as Node, { | ||
| enter(node) { | ||
| if (node.type === C.ImportDeclaration && node.source.value === C.NodeTest) { | ||
| if (node.type === C.ImportDeclaration && node.source.value === "../utils") { |
There was a problem hiding this comment.
Instead of this hard-coded value here, it would read the setting and determine if the import matches the configuration.
|
Sure, I'm good for this. As you said it should be configurable. I would probably just allow a regex expression to specify the import, const defaultValue = {
from: "^node:test$",
};
const yours = {
from: "test/utils\\.ts$",
}We actually don't look at the function names currently, so this doesn't need to be specified. |
|
OK that sounds good - I think allowing an array of regex is probably a bit better, as it allows slow adoption into
Hmm, I think I'd need to add that right? At least for me, my test/utils.ts file has more than 1 function exported from it. Only one of them actually defines a test. I think if I understand the code, it is something like this: "a function imported from node:test that has a string as its first argument". I guess if you don't want to add "name matching" that's fine, I can work with that. |
|
I'm okay if we want to add an array of names to serve as an allowlist. I just didn't need it before. |
|
OK, I think I'll get started on this. I'm new to developing extensions, if you have any must-read tutorials or pro-tips please send them over. |
|
You should be able to clone this repo, run |
|
OK, I took a first stab at this here: |
|
Closing in favor of that PR |
Context
My company has a 10 year old node.js codebase, and we have recently converted it to use
node:test. However, in order to successfully migrate, and for future-proofing the testing - we have wrapped thetestfunction up in our own function.We have started to work on making the codebase accessible to very junior developers, and wanted to have "click to run tests" functionality, and stumbled upon this extension. It is honestly great, but changing all of our tests to use
node:testdirectly is a bit too much to ask.Request up for discussion
I'd like to add a setting / configuration that allows for specifying a "test function override" - where you can specify where your test wrapper function is (instead of
import { test } from 'node:test';it might beimport { test } from './utils';for example ).This would allow us to use this extension for our testing with minimal changes to the codebase, and provide a single place to update if we ever switched to vitest (for example) or jest or whatever new testing framework comes around in 10 more years.
Other examples
We hit a similar problem with typescript-eslint and I thought their solution to it was elegant, where they allow specifying a callable function with a TypeOrValueSpecifier .
Proposal
I think nodejs-testing could adopt a setting that accepted a TypeOrValueSpecifier. The default value of this setting would be:
But anyone who wanted to wrap the test function would replace it with the
TypeOfValueSpecifierthat matched their wrapped test function.The code I attached to this was a very very hacky proof of concept I did to ensure it would work in theory, it is by no means a suggestion to merge this code. I just wanted to have a discussion before doing the "real" implementaiton.