Open Lycolia opened 8 months ago
I have the same problem, I looked at this issue and apparently the native node test does not have support for mocking modules https://github.com/nodejs/help/issues/4298
Odd note, when I do:
import fs from 'node:fs'
import * as blah from './blah';
test('my test', (t) => {
// this works
t.mock.method(fs, 'writeFileSync', () => /* */);
// this doesn't work
t.mock.method(blah, 'someExport', () => /* */);
});
I'm guessing this is either because I'm using a loader (node --loader tsx mytest.test.ts
), or the node:fs
module is actually exporting an object, whereas blah.ts
just has export function thing() {}
.
In any case, I would also like to be able to mock exports from local files
Details
I'm trying to mock a function that has been exported using named export, but I'm not able to do it successfully. How can I mock it properly?
Any help would be greatly appreciated!
Expected Behavior
test passing
Actual Behavior
Node.js version
v20.0.0
Example code
Implementation Code
Test Code
Operating system
Ubuntu 22.04.3 LTS
Scope
test
Module and version