node icon indicating copy to clipboard operation
node copied to clipboard

test_runner: support module mocking

Open cjihrig opened this issue 1 year ago • 4 comments

This commit adds experimental module mocking to the test runner.

Fixes: https://github.com/nodejs/node/issues/51164

cjihrig avatar May 05 '24 17:05 cjihrig

Review requested:

  • [ ] @nodejs/test_runner
  • [ ] @nodejs/web-infra

nodejs-github-bot avatar May 05 '24 17:05 nodejs-github-bot

@nodejs/loaders might wanna take a look too

benjamingr avatar May 05 '24 18:05 benjamingr

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.

cjihrig avatar May 05 '24 19:05 cjihrig

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).

benjamingr avatar May 06 '24 10:05 benjamingr

CI: https://ci.nodejs.org/job/node-test-pull-request/59244/

nodejs-github-bot avatar May 16 '24 13:05 nodejs-github-bot

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 avatar May 16 '24 14:05 MoLow

@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

cjihrig avatar May 16 '24 14:05 cjihrig

CI: https://ci.nodejs.org/job/node-test-pull-request/59257/ 🟡

nodejs-github-bot avatar May 16 '24 21:05 nodejs-github-bot

This needs a re-approval in order to land via the commit queue.

cjihrig avatar May 18 '24 14:05 cjihrig

Landed in a619789ef019301ed1bbc2a0b35f9b4b32fe2667

nodejs-github-bot avatar May 19 '24 05:05 nodejs-github-bot

4983546f3ec166eebcd2fb84c6fd1fd10706523a@@

hossain666 avatar May 25 '24 20:05 hossain666

Types added too in this PR 👍🏼

rozzilla avatar Jun 27 '24 08:06 rozzilla