How to test a class method with Jest
September 1, 2020
How to test if a method has been called:
class Person {
greet() {
console.log("Hi!");
}
}
test("greet has been called", () => {
const person = new Person();
// We have to spy on the instance of the Person class because jest.spyOn expects an object as its first argument
const spyOnGreet = jest.spyOn(person, "greet");
person.greet();
expect(spyOnGreet).toHaveBeenCalled();
});
How to test a method that gets called in the constructor:
class Person {
constructor() {
this.greet();
}
greet() {
console.log("Hi!");
}
}
test("greet has been called", () => {
// Now we have to spy on the prototype of the Person class because jest.spyOn expects an object as its first argument
const spyOnGreet = jest.spyOn(Person.prototype, "greet");
const person = new Person();
expect(spyOnGreet).toHaveBeenCalled();
});