Skip to content

Instantly share code, notes, and snippets.

@jcuffe
Last active August 3, 2023 20:18
Show Gist options
  • Save jcuffe/568175435259a657931a94e542f633b5 to your computer and use it in GitHub Desktop.
Save jcuffe/568175435259a657931a94e542f633b5 to your computer and use it in GitHub Desktop.
ESM internal module mocks
import { __testVisible as self } from './index.mjs';
function a() {
return 1;
}
export function b() {
return self.a();
}
export const __testVisible = {
a,
};
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);
});
});
❯ 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