node icon indicating copy to clipboard operation
node copied to clipboard

type stripping + module mocking causes test runner to throw

Open JakobJingleheimer opened this issue 6 months ago • 5 comments

Version

22.6.0

Platform

Irrelevent

Subsystem

module

What steps will reproduce the bug?

  1. Create a typescript file (ex logger.ts)
  2. Create a test file (ex replace-js-ext-with-ts-ext.test.ts)
  3. Within the test file, set up a module mock
    const logger = mock.fn();
    
    before(async () => {
      mock.module('./logger.ts', {
        namedExports: { logger }
      });
    
      await import('./replace-js-ext-with-ts-ext.ts');
    });
    
  4. Run the test (ex node --experimental-test-module-mocks --experimental-strip-types --test ./replace-js-ext-with-ts-ext.test.ts)

How often does it reproduce? Is there a required condition?

100%

What is the expected behavior? Why is that the expected behavior?

It should behave the same as when the module mock target is a node builtin or a javascript file.

What do you see instead?

  TypeError [ERR_INVALID_ARG_VALUE]: The argument 'format' must be one of: 'builtin', 'commonjs', 'module'. Received 'module-typescript'
      at MockTracker.module (node:internal/test_runner/mock/mock:517:7)
      at SuiteContext.<anonymous> (file:///[…]/JakobJingleheimer/correct-ts-specifiers/replace-js-ext-with-ts-ext.test.ts:37:23)
      at TestHook.runInAsyncScope (node:async_hooks:206:9)
      at TestHook.run (node:internal/test_runner/test:865:25)
      at TestHook.run (node:internal/test_runner/test:1116:18)
      at TestHook.run (node:internal/util:545:20)
      at node:internal/test_runner/test:785:20
      at async Suite.runHook (node:internal/test_runner/test:783:7)
      at async Suite.run (node:internal/test_runner/test:1220:7)
      at async Test.processPendingSubtests (node:internal/test_runner/test:574:7) {
    code: 'ERR_INVALID_ARG_VALUE'
  }

Additional information

No response

JakobJingleheimer avatar Aug 17 '24 22:08 JakobJingleheimer