Open tlan16 opened 7 months ago
I am wondering the same thing.
From what I could see online, people usually show how to mock a function that exists within the test file, such as:
const sum = mock.fn((a, b) => {
return a + b;
});
But I could not find an example of how to mock an exported function, as mentioned by @tlan16, and thus am wondering if it is even possible.
I think it's fine mocking named export is not something node test runner aids to support. If so, will be nice make it clear in the doc.
Something like this works:
const A = { MyFunc: () => {} }
export default A
import A from "./A.js"
mock.method(A, "myFunc", () => Promise.resolve());
The caveat being the default export is mockable. I have not found a way to mock individual export.
Is this issue still occuring? If not, feel free to self-close.
Details
Hi, I'm trying out the new test runner from node. The intention is to remove dependency on jest and hopefully faster test execution.
I setup a proof of concept repo here: https://github.com/tlan16/node-typescript-app-template/tree/764d91f0cdfab0dcd4d2af5d5e9b04d6bcad5053
In the test here, I wanna mock the named export
getWelcomeMessage
to return a different value, but somehow not effective, aka test fails.Node.js version
20.9.0
Example code
https://github.com/tlan16/node-typescript-app-template/blob/764d91f0cdfab0dcd4d2af5d5e9b04d6bcad5053/src/__tests__/getWelcomeMessage.spec.ts#L16
Operating system
Mac OS Ventura 13.6.1
Scope
test runner
Module and version
package.json:
tsconfig.json: