Encountering a problem while writing unit tests for a function with instance checks. The business logic is as follows:
let status = new B();
const testFunction = () => {
if (status instanceof A) {
console.log('inside A')
} else if (status instanceof B) {
console.log('inside B')
} else {
console.log('No matchers')
}
}
Unit Test:
describe('test', ()=>{
it("should test b", ()=>{
let status = new A()
expect(status).toBeInstanceOf(A) // passes
expect(logSpy).toHaveBeenCalledWith('inside A') // fails and expects 'inside B'
})
})
Believe the test fails because the original file returns an instance of class B for 'status', causing it to match the 'else if' condition instead of the 'if' condition. Unsure how to test this without changing 'status'. Am I missing something?
EDIT: Assuming testFunction() is already invoked in unit tests