Answers for "jest spyon"

3

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'
Posted by: Guest on June-10-2020
-1

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)
})
Posted by: Guest on November-20-2020

Code answers related to "Javascript"

Browse Popular Code Answers by Language