test_runner: support module mocking
This commit adds experimental module mocking to the test runner.
Fixes: https://github.com/nodejs/node/issues/51164
Review requested:
- [ ] @nodejs/test_runner
- [ ] @nodejs/web-infra
@nodejs/loaders might wanna take a look too
the ergonomics bother me a bit
What would you like the ergonomics to look like? ESM is kind of restrictive here in ways that CJS is not. Is there a C++ API I can leverage to uncache or change an existing ES module? If so, I would happily use that instead.
What would you like the ergonomics to look like? ESM is kind of restrictive here in ways that CJS is not. Is there a C++ API I can leverage to uncache or change an existing ES module? If so, I would happily use that instead.
In big'ish new features I'm in the camp of "land and iterate" hence why I read then approved this PR. There is time for API bikeshedding later IMO for this sort of active development work.
That said, it would be nice if:
- The code warned when you .mock a module that was already loaded
- The code examples showed a more complete example of mocking through a third file (so imports only happen after .mock happened)
I have no better API ideas (other than changing the original module since it's live bindings which is bad and we shouldn't do).
CI: https://ci.nodejs.org/job/node-test-pull-request/59244/
Maybe in a future PR we could expose this in a way that other test frameworks could use it too?
it is already exposed. you can import { mock } from 'node:test' and use a different framework for the actual testing
@MoLow I cancelled your CI run because it's going to fail on Windows: https://ci.nodejs.org/job/node-test-binary-windows-js-suites/27754/RUN_SUBSET=3,nodes=win11-arm64-COMPILED_BY-vs2022-arm64/console
CI: https://ci.nodejs.org/job/node-test-pull-request/59257/ 🟡
This needs a re-approval in order to land via the commit queue.
Landed in a619789ef019301ed1bbc2a0b35f9b4b32fe2667
4983546f3ec166eebcd2fb84c6fd1fd10706523a@@
Types added too in this PR 👍🏼