jest spyon
// jest.spyOn(object, methodName)
const spy = jest.spyOn(video, 'play');
// jest.spyOn(object, methodName, accessType?)
const spy = jest.spyOn(video, 'play', 'get'); // we pass 'get'
jest spyon
// jest.spyOn(object, methodName)
const spy = jest.spyOn(video, 'play');
// jest.spyOn(object, methodName, accessType?)
const spy = jest.spyOn(video, 'play', 'get'); // we pass 'get'
jest spy on class method
//class.js
class MyClass {
methodOne() {
return 1;
}
methodTwo() {
return 2;
}
}
module.exports = MyClass;
// class.test.js
test('spy using class method', () => {
const result = new MyClass()
const spy = jest.spyOn(result, 'methodOne')
result.methodOne()
// check class method is call or not
expect(spy).toHaveBeenCalled()
// expect old value
expect(result.methodOne()).toBe(1)
// expect new value
spy.mockReturnValueOnce(12)
expect(result.methodOne()).toBe(12)
})
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us