Skip to content
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

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

Open
cu8code opened this issue Nov 17, 2024 · 4 comments
Open

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

cu8code opened this issue Nov 17, 2024 · 4 comments
Labels
test_runner Issues and PRs related to the test runner subsystem.

Comments

@cu8code
Copy link

cu8code commented Nov 17, 2024

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

@RedYetiDev RedYetiDev added confirmed-bug Issues with confirmed bugs. test_runner Issues and PRs related to the test runner subsystem. labels Nov 17, 2024
@cjihrig
Copy link
Contributor

cjihrig commented Nov 17, 2024

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

@MoLow MoLow removed the confirmed-bug Issues with confirmed bugs. label Nov 17, 2024
@cu8code
Copy link
Author

cu8code commented Nov 18, 2024

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!

@MoLow
Copy link
Member

MoLow commented Nov 18, 2024

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

@cjihrig
Copy link
Contributor

cjihrig commented Nov 18, 2024

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.

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
None yet
Development

No branches or pull requests

4 participants