Prejsť na hlavný obsah
← ⚡ JavaScript & the browser·Modul C8 · Lekcia 6
ÚlohaBuild spy() returning a function with .calls = [] and .returnValue. Calling the spy pushes args to .calls and returns .returnValue. Set s.returnValue = 42, call s('a', 'b') twice, log s.calls.length, s.calls[0].join(','), s('x') === 42.

Spies — recording calls

150 XP10 min
Teória

When you need to know how a function called its dependencies

A spy is a fake function that records every call and lets you assert on them later.

function spy() {
  const fn = function (...args) {
    fn.calls.push(args);
    return fn.returnValue;
  };
  fn.calls = [];
  fn.returnValue = undefined;
  return fn;
}

Usage:

const onSave = spy();
onSave.returnValue = "ok";

processForm(formData, onSave);

assert(onSave.calls.length === 1, "save called once");
assertEqual(onSave.calls[0][0].name, "Anna", "first arg was formData");

Why this is enough

Vitest's vi.fn() and Jest's jest.fn() are this exact shape with bells (.mockImplementation, .mockReturnValueOnce, .mockResolvedValue). For 80% of tests, a vanilla spy is plenty.

🔒

Zaregistruj sa a začni programovať

Teória je otvorená pre každého. Editor, živý náhľad a kontrola sa odomknú so 7-dňovou bezplatnou skúškou — karta potrebná, zruš kedykoľvek.

Registrácia — bezplatná skúška →

Prvých 10 lekcií v každej trase je zdarma. Karta nie je potrebná.

← PredchádzajúcaĎalšia lekcia →

Jeden tip k Pythonu alebo webu denne — e-mailom

Krátke, ručne písané, žiadny spam. Odhlásiť jedným klikom.