Перейти до основного вмісту
← ⚡ JavaScript & the browser·Модуль C8 · Урок 5
ЗавданняAdd describe(name, fn) that nests its tests under "<parent> > <name>". Inside describe('cart', () => {}) register 'totals zero when empty' (assert([].reduce((s,x)=>s+x,0)===0)) and 'sums non-empty' (assert([1,2,3].reduce((s,x)=>s+x,0)===6)). runAll() should print both with the 'cart > ' prefix.

Grouping with describe()

125 XP9 min
Теорія

Nested test groups

describe(name, fn) groups related tests. The runner shows their names with the parent prefix, so failures are organized.

let currentSuite = "";
function describe(name, fn) {
  const prev = currentSuite;
  currentSuite = prev ? prev + " > " + name : name;
  fn();
  currentSuite = prev;
}

function test(name, fn) {
  tests.push({ name: currentSuite ? currentSuite + " > " + name : name, fn });
}

Now you can:

describe("cart", () => {
  test("totals zero when empty", () => { ... });
  test("applies discount", () => { ... });
});

Output:

PASS cart > totals zero when empty
PASS cart > applies discount

Nested describes

describe can call describe. The current-suite variable is restored when each block returns, so siblings don't see each other's prefix.

🔒

Зареєструйся щоб кодувати

Теорія відкрита всім. Інтерактивний редактор, живий перегляд і перевірка відкриваються з 7-денним безкоштовним trial — потрібна картка, скасування у будь-який час.

Реєстрація — безкоштовний trial →

Перші 10 уроків у кожному треку — безкоштовні. Картка не потрібна.

← НазадНаступний урок →

Отримуй одну пораду з Python чи вебу — щодня поштою

Коротко, без води, без спаму. Відписка — в один клік.