I have a non object function
on getConfig.js
export default function () {
/** do something **/
return {
appConfig: { status: true }
}
}
and under my main file
import getConfig from './getConfig';
export default function() {
const { appConfig } = getConfig();
if(appConfig.status) {
console.log("do the right thing");
else {
console.log("do other things")
}
}
}
How do I mock the getConfig function using sinon stub or mock methods? or is there any better way to set appConfig.status property to make true or false?
I would like to do the following but its not working
import getConfig from './getConfig';
import main from './main';
test('should status to be true', () => {
sinon.stub(getConfig).callsFake(sinon.fake.returns({status:true});
expect(main()).toBe('to do the right thing')
}
Ok I found another alternate solution using just JEST
jest.mock('./getConfig', () => (
jest.fn()
.mockReturnValueOnce({ appConfig: { status: true }})
.mockReturnValueOnce({ appConfig: { status: false }})
))
//The getConfig get replaced with the above mock function
So the solution looks like
import main from './main';
jest.mock('./getConfig', () => (
jest.fn()
.mockReturnValueOnce({ appConfig: { status: true }})
.mockReturnValueOnce({ appConfig: { status: false }})
))
test('should status to be true', () => {
expect(main()).toBe('to do the right thing')
expect(main()).toBe('to do the other things')
}