Bun 의 테스트 러너에서 메서드 호출을 추적하려면 spyOn 유틸리티를 사용하세요.
ts
import { test, expect, spyOn } from "bun:test";
const leo = {
name: "Leonardo",
sayHi(thing: string) {
console.log(`Sup I'm ${this.name} and I like ${thing}`);
},
};
const spy = spyOn(leo, "sayHi");스파이가 생성되면 메서드 호출과 관련된 expect 어설션을 작성하는 데 사용할 수 있습니다.
ts
import { test, expect, spyOn } from "bun:test";
const leo = {
name: "Leonardo",
sayHi(thing: string) {
console.log(`Sup I'm ${this.name} and I like ${thing}`);
},
};
const spy = spyOn(leo, "sayHi");
test("turtles", () => {
expect(spy).toHaveBeenCalledTimes(0);
leo.sayHi("pizza");
expect(spy).toHaveBeenCalledTimes(1);
expect(spy.mock.calls).toEqual([["pizza"]]);
}); Bun 테스트 러너로 모의에 대한 전체 문서는 문서 > 테스트 러너 > 모의 를 참조하세요.