Skip to content

--experimental-test-module-mocks is not working as expected #55891

Open

Description

@0hmX

Version

v24.0.0-pre

Platform

Linux fedora 6.11.6-200.fc40.x86_64 #1 SMP PREEMPT_DYNAMIC Fri Nov  1 16:09:34 UTC 2024 x86_64 GNU/Linux

Subsystem

No response

What steps will reproduce the bug?

This issue was observed inside the nodejs source code, when tying to create a benchmark
create a file benchmark/test_runner/mock-module.js

now copy this code

"use strict";

const { test } = require("node:test");

function main() {
  test(async (t) => {
    console.log("benchmark");

    try {
    // Create a mock module
    t.mock.module('axios', {
      namedExports: {
        get: (url) => url
      }
    });

    } catch (e) {
      console.error(e);
    }

    console.log("end");
  });
}

main()

now run this using ./node --experimental-test-module-mocks benchmark/test_runner/mock-module.js

You will get this output

benchmark
Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'axios' imported from /home/ankan/Documents/git/me/node/benchmark/test_runner/mock-module.js
    at Object.getPackageJSONURL (node:internal/modules/package_json_reader:267:9)
    at packageResolve (node:internal/modules/esm/resolve:768:81)
    at moduleResolve (node:internal/modules/esm/resolve:854:18)
    at defaultResolve (node:internal/modules/esm/resolve:984:11)
    at nextResolve (node:internal/modules/esm/hooks:748:28)
    at resolve (node:internal/test_runner/mock/loader:78:35)
    at nextResolve (node:internal/modules/esm/hooks:748:28)
    at Hooks.resolve (node:internal/modules/esm/hooks:240:30)
    at handleMessage (node:internal/modules/esm/worker:199:24)
    at Immediate.checkForMessages (node:internal/modules/esm/worker:141:28) {
  code: 'ERR_MODULE_NOT_FOUND'
}
end
✔ <anonymous> (42.524333ms)
(node:91728) ExperimentalWarning: Module mocking is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
ℹ tests 1
ℹ suites 0
ℹ pass 1
ℹ fail 0
ℹ cancelled 0
ℹ skipped 0
ℹ todo 0
ℹ duration_ms 48.749785

As you can we we are trying to create a mock 'axios' module but we receive the error Cannot find package 'axios', which is unexpected as we are not even importing the module. The error is occurring because of the line t.mock.module

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

dose not need any special condition

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

should create a mock module called axios

What do you see instead?

their is an error, saying package 'axios' not found, which is very unexpected

Additional information

No response

Activity

  1. added
    confirmed-bugIssues and PRs for confirmed bugs.
    test_runnerIssues and PRs related to the test runner subsystem.
    on Nov 17, 2024
  2. cjihrig commented on Nov 17, 2024

    @cjihrig
    Contributor

    Is axios actually installed, or are you trying to mock a module that does not exist?

  3. 0hmX commented on Nov 18, 2024

    @0hmX
    ContributorAuthor

    I realize I made a bit of a mess earlier and completely overlooked an important detail: the package actually needs to be installed! Totally makes sense, but for some reason, I thought we could just create a random module and mock its implementation out of thin air. This issue should probably be closed, but I’ll leave that decision to the maintainers. Apologies to everyone for the incovinience!

  4. MoLow commented on Nov 18, 2024

    @MoLow
    Member

    Maybe we should add a comment regarding that in the documentation (assuming there isn't one already)

  5. cjihrig commented on Nov 18, 2024

    @cjihrig
    Contributor

    We should also decide if we want to support mocking modules that do not exist. I don't think there is a technical reason we couldn't do it, but I don't know if that is something people actually want/need to do in the wild.

  6. alltom commented on Jul 2, 2025

    @alltom

    This just bit me. :) Two things:

    1. Can mock.module log a message when you try to mock one that doesn't exist? Then I wouldn't have needed to find this thread to understand what was going on. As it stands, mock.module appears to succeed and the only thing in the logs is the "cannot find package" message.
    2. I'd find it useful if you could mock a module that doesn't exist. I'm writing unit tests for an Obsidian plugin, which specifies "obsidian" (and "electron", etc) as external dependencies in the esbuild context because they're provided when the plugin is run in the Obsidian app. But I want to be able to test files that import "obsidian" too.
  7. pmarchini commented on Jul 2, 2025

    @pmarchini
    Member

    I think we should support this use case.
    Specifically, I also believe that adding a parameter to throw an exception when mocking a non-existent dependency could be very useful.
    Personally, I think it should be the default behaviour, but it would introduce a breaking change that shouldn't be underestimated.

  8. alltom commented on Jul 18, 2025

    @alltom

    In case it helps others:

    I've been working around this by creating a module named obsidianWrapper that simply contains export * from 'obsidian';, importing that everywhere instead of 'obsidian' proper, and mocking obsidianWrapper in tests instead.

  9. filmaj commented on Sep 4, 2025

    @filmaj

    I also think mocking non-existent modules should be supported. My use case: I work for a company that provides a function-as-a-service feature. Our company maintains a "function wrapper" that imports userland code. In tests of this function wrapper, I would like to mock the userland code being imported - even if it doesn't exist.

  10. github-actions commented on Apr 26, 2026

    @github-actions
    Contributor

    This issue has been marked as stale due to 210 days of inactivity.
    It will be automatically closed in 30 days if no further activity occurs. If this is still relevant, please leave a comment or update it to keep it open.

  11. added
    staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.
    on Apr 26, 2026
  12. filmaj commented on Apr 26, 2026

    @filmaj

    Please do not mark this as stale 😄 🙏

  13. removed
    staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.
    on Apr 27, 2026
  14. github-actions commented on Jul 27, 2026

    @github-actions
    Contributor

    This issue has been marked as stale due to 90 days of inactivity.
    It will be automatically closed in 30 days if no further activity occurs. If this is still relevant, please leave a comment or update it to keep it open.

  15. added
    staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.
    on Jul 27, 2026
  16. filmaj commented on Jul 28, 2026

    @filmaj

    Not stale dearest GitHub Robot 🙏

  17. removed
    staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.
    on Jul 29, 2026
  18. github-actions commented on Oct 28, 2026

    @github-actions
    Contributor

    This issue has been marked as stale due to 90 days of inactivity.
    It will be automatically closed in 30 days if no further activity occurs. If this is still relevant, please leave a comment or update it to keep it open.

  19. added
    staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.
    on Oct 28, 2026
  20. alltom commented on Oct 28, 2026

    @alltom

    I still think about this every time I have to import "obsidianWrapper" instead of "obsidian".

  21. removed
    staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.
    on Oct 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    test_runnerIssues and PRs related to the test runner subsystem.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions