spyOn ユーティリティを使用して Bun のテストランナーでメソッド呼び出しを追跡します。
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 テストランナーでのモックの完全なドキュメントについては、ドキュメント > テストランナー > モック を参照してください。