Skip to content

mock.module: add preserveOthers config option #58444

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
JakobJingleheimer opened this issue May 24, 2025 · 0 comments
Open

mock.module: add preserveOthers config option #58444

JakobJingleheimer opened this issue May 24, 2025 · 0 comments
Assignees
Labels
test_runner Issues and PRs related to the test runner subsystem.

Comments

@JakobJingleheimer
Copy link
Member

JakobJingleheimer commented May 24, 2025

This would simplify mocking only a subset of a module's exports. A common use-case for this is a method that will do something undesirable in a test environment, like start a server.

Currently, it's possible with some non-obvious gymnastics:

describe('Example', () => {
  let example;

  before(async () => {
    example = await import('./example.mjs');

    const theMock = mock.module('./example.mjs', {
      namedExports: {
        ...example,
        theOneThingToMock: mock.fn(),
      },
    });

    // Must do this again!
    example = await import('./example.mjs');
  });
});

Instead, with preserveOthers, it's more straightforward:

describe('Example', () => {
  let example;

  before(async () => {
    const theMock = mock.module('./example.mjs', {
      namedExports: {
        theOneThingToMock: mock.fn(),
      },
      preserveOthers: true,
    });

    example = await import('./example.mjs');
  });
});

There is some hidden complexity in the implementation though: When a module is loaded, it’s immediately added to the module cache (which is controlled by V8—node can’t manipulate it).

So we’ll need to do some trickery like initially load the module with a query param appended to the specifier (which may already have a query param of its own 🤪), grab the exports and cache them for as long as the module is in scope: A module can be mocked multiple times, potentially with different replacements. We’ll need to diff replacements and original.


This would also facilitate supporting a feature like Jest's requireActual: theMock.getOriginal.

@JakobJingleheimer JakobJingleheimer self-assigned this May 24, 2025
@JakobJingleheimer JakobJingleheimer added the test_runner Issues and PRs related to the test runner subsystem. label May 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
test_runner Issues and PRs related to the test runner subsystem.
Projects
Status: Todo
Development

No branches or pull requests

1 participant