Last active
August 3, 2023 20:18
-
-
Save jcuffe/568175435259a657931a94e542f633b5 to your computer and use it in GitHub Desktop.
ESM internal module mocks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { __testVisible as self } from './index.mjs'; | |
function a() { | |
return 1; | |
} | |
export function b() { | |
return self.a(); | |
} | |
export const __testVisible = { | |
a, | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import chai from 'chai'; | |
import sinon from 'sinon'; | |
import { b, __testVisible } from './index.mjs'; | |
describe('mocks', function () { | |
before(function () { | |
sinon.stub(__testVisible, 'a'); | |
}); | |
afterEach(function () { | |
sinon.reset(); | |
}); | |
it('calls through', function () { | |
__testVisible.a.callThrough(); | |
chai.expect(b()).eql(1); | |
}); | |
it('mocks', async function () { | |
__testVisible.a.returns(2); | |
chai.expect(b()).eql(2); | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
❯ npx mocha index.test.mjs | |
mocks | |
✔ calls through | |
✔ mocks | |
2 passing (4ms) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment