Skip to content

Usa l'utilità spyOn per tracciare le chiamate ai metodi con il test runner di 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");

Una volta creato lo spy, può essere usato per scrivere asserzioni expect relative alle chiamate dei metodi.

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("tartarughe", () => { 
  expect(spy).toHaveBeenCalledTimes(0); 
  leo.sayHi("pizza"); 
  expect(spy).toHaveBeenCalledTimes(1); 
  expect(spy.mock.calls).toEqual([["pizza"]]); 
}); 

Vedi Docs > Test Runner > Mocks per la documentazione completa sulla simulazione con il test runner di Bun.

Bun a cura di www.bunjs.com.cn