-
Notifications
You must be signed in to change notification settings - Fork 284
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
Native test runner: document how to mock standalone imported functions #4298
Comments
As an example, what jest does is that it allows you to mock a module by path: jest.mock("../add", () => ({
add: jest.fn(),
}) |
I also realised this has been discussed here: nodejs/node#42242 |
Further findings |
Hi @GabeAtWork I'm facing a similar issue, where want to mock an external lib function. but it's always making a real call to function and returning the actual result instead of a mocked result.
Do you have a solution for this? I don't want to use any lib other than default NodeJs test runner. |
Hi @anandkhatri, AFAIK there is no known workaround that doesn't involve using a third-party lib or rolling out your own module mocking function. |
I'm also struggling with this. I'm trying to use I would like to be able to use import * as fetch from 'node-fetch-cache';
...
mock.method(fetch, 'default', () => Promise.resolve(response)); But I get the error
I've been using esmock to work around this, but it feels like something that the node test runner is able to support without a third-party library and I just haven't figured out how yet. |
Note: there is now an issue for this on the node repo itself: nodejs/node#51164 |
This makes mocking the functionality much easier; see <nodejs/help#4298>. Co-Authored-By: Alice Fage <[email protected]>
#918) #### Motivation This makes mocking the functionality much easier; see <nodejs/help#4298>. #### Checklist - [ ] Tests updated (N/A) - [ ] Docs updated (N/A) - [x] Issue linked in Title Co-authored-by: Alice Fage <[email protected]>
Creator of cjs-mock here. I'm not convinced that it's a test runner's job to do mocking. Their job is just to run tests, not care about what is in them. Sure, it needs to support async, etc. but mocking seems a bit heavy of a lift and dependent on the module type (ie, CJS, ESM, AMD, etc.). I also created a test runner, hoare, which does not do mocking. There's also sinon, which is more about building fake objects than module importing but useful in mocking. I haven't dabbled much with ESM testing yet (I'm using Typescript so I'm a bit insulated) but will work on an ESM version if there is enough interest. If anyone has any advice on how to make cjs-mock or hoare any better, I'd love to hear from you (or collaborate). I'm also able to answer questions! I'm really passionate about both projects and testing in general. My background is in safety-critical software, working in healthcare and aerospace industries. |
Hi @doug-wade, Have you tried using the default import I encountered the same error on Node version import childProcess from 'node:child_process';
test('some-test', () => {
const mockedExecSync = mock.method(childProcess, 'execSync', () => {});
...
}); From Node version |
I’m trying to do basically exactly the same thing— |
Details
Currently, when adopting the native test runner, it is very hard to understand how to mock standalone functions used in the unit under test. Judging by this SO question, I'm not the only one who can't figure this out 😄
None of the documentation examples on the Test runner demonstrate the mocking of imported functions.
The only way I found to mock functions used in another function is to export all functions of the depended-on function's file, e.g.
export default {all, my, functions}
. From a production code standpoint, it's both unnecessary (if I want to only use one function from a file, why should I need to import the whole default and access the function I need as a member?) and in some contexts undesirable (my gut feeling is that it would make treeshaking impossible, but I haven't verified this).You'll find an example in this repository: https://github.com/GabeAtWork/node-test-runner-cannot-mock-function-minimal-example
The file demonstrates 3 approaches:
mock.method
(works, but is not desirable as explained above)mock.fn
(our go-to way of writing code, but doesn't mock the function, so the actual function gets called)mock.method
(would be an ok compromise, but it seems to fail in what looks like a bug)It may be that there is an alternate way of solving this problem which I haven't found yet, so please do let me know if it exists! Since this feature is so fresh, there's not a lot of content on it yet. Blog posts and videos I found don't actually show functions imported from different files or only show default imports and classes being mocked.
Feature request on the node repo: nodejs/node#51164
Node.js version
20.8.0
Example code
You can find a minimal example in this repository: https://github.com/GabeAtWork/node-test-runner-cannot-mock-function-minimal-example
Operating system
MacOS (but not relevant)
Scope
Documentation / API design
Module and version
Not applicable.
The text was updated successfully, but these errors were encountered: