let's say I have a module that needs to be initialized once in the start of the app (to pass on configuration). module will look something like this :
MyModule.js
let isInitiazlied;
const myModule = {
init: function() {
isInitiazlied = true;
},
do: function() {
if (!isInitiazlied)
throw "error"
//DO THINGS
}
}
export default myModule;
I want to unittest it, using jest. test file looks something like this :
MyModule.test.js
import myModule from './MyModule'
describe('MyModule', () => {
describe('init', () => {
it('not throws exception when called', () => {
expect(() => myModule.init()).not.toThrow();
});
})
describe('do', () => {
it('throw when not init', () => {
expect(() => myModule.do()).toThrow();
});
})
})
when I run the test, the 2nd test fail, as the module already initialized so the exception is not thrown.
I tried using jest.resetModules() in beforeEach, but that didn't work.
Is there a way to solve it (different module pattern/test case) ?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…