-
Notifications
You must be signed in to change notification settings - Fork 20
Description
If I use import: in esmock to mock a global it messes up the coverage report. At least it does when run with c8 mocha. I constructed a minimal test scenario for it below:
Here's an example source file src/myfile.js:
function someFun() {
console.log('something');
}
export async function testFun() {
console.log('test');
return 418;
}
And here's an example test for it. It only tests the testFun method, but it has a mock defined for the global fetch (which isn't actually used):
import assert from 'assert';
import esmock from 'esmock';
describe('ESMock test case', () => {
it('Simple test', async () => {
const { testFun } = await esmock(
'../src/myfile.js', {
import: { fetch: async (url) => {} }
});
assert.strictEqual(418, await testFun());
})
});
When I run this and look at the coverage report it looks like this:

The above coverage report is clearly wrong.
If I comment out the line with the import, e.g.:
const { testFun } = await esmock(
'../src/myfile.js', {
// import: { fetch: async (url) => {} }
});
the coverage report is correct again:

Note that using esmock with things other than globals, doesn't have this issue, e.g. the following doesn't cause this issue:
const { postSource } = await esmock(
'../../src/source.js', {
'../../src/included.js': {
default: mockResp
}
});
It would be great if I can use the globals mocking without messing up the coverage report 😄