Reputation: 493
This has bothered me for a while. I have two functions in the same file.
//fun.ts
export function fun1(){
let msg = fun2();
return msg;
}
export function fun2(): string{
return "Some message";
}
I have a typescript spec that stubs fun2 and calls fun1.
//fun.spec.ts
import * as Fun from 'fun';
describe('Stubing', () => {
it('should stub the return value', () => {
spyOn(Fun, 'fun2').and.returnValue("A different message");
expect(Fun.fun1()).toEqual("A different message")
});
});
But when I run the spec, the output I get is
Failures:
1) Stubing should stub the return value
1.1) Expected 'Some message' to equal 'A different message'.
I wrote the tests in typescript and then I have a gulp script that successfully transpiles and runs the jasmine specs. Everything works, the only thing that I can't figure out is why the spy is not working. An explanation would be appreciated.
Upvotes: 4
Views: 2358
Reputation: 493
I finally figured this out. In fun.ts, I am directly calling the fun2 object, but my Jasmine spec has no access to that object. The only object the Jasmine spec can access is the exports object. If I want the spy to work I need to call fun2 on the exports object.
//fun.ts
export function fun1(){
let msg = exports.fun2();
console.log(msg);
}
export function fun2(): string{
return "Some message";
}
Now when the spec executes I see
.
1 spec, 0 failures
Upvotes: 9