jest spy on class method
test("es6 class", () => {
const SomeClass = jest.fn();
const mMock = jest.fn();
SomeClass.mockImplementation(() => {
return {
m: mMock
};
});
const some = new SomeClass();
some.m("a", "b");
expect(mMock.mock.calls).toEqual([["a", "b"]]);
});//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)
})
Also in JavaScript:
- js is undefined
- open popup window javascript
- react get screen width
- nexusjs file upload
- email collapsible section
- React Hook useEffect has a missing dependency:'. Either include it or remove the dependency array.
- get left position based on container jquery
- npm ERR! Unexpected end of JSON input while parsing near '...ty":"sha512-2GTXt3b2Q'
- node express table view
- jquery check input is disable
- get date format javascript
- jquery get
- js code to run hello world
- $("#symptomSelector").symptomSelector WHAT DOES THIS MEAN IN JAVASCRIPT
- js for of loop
- react native passing params to nested navigators
- can you get reinfected with the coronavirus
- jQuery - Get Content and Attributes
- create react project
- scrollout js angular
- how to add field to object in js
- node.js version
- redirect with javascript to another page
- checking if a user has a role in discord js