All quizzesEasy
Unit Testing — Series 3
Preview — 3 of 10 questions
This test passes. What is it failing to verify?
javascript
it('adds an item to the cart', () => {
const cart = new Cart();
cart.add({ id: 1, price: 10 });
cart.add({ id: 2, price: 5 });
});AArrange — the cart is never set up.
BAssert — the test performs actions but never checks an outcome, so it passes as long as nothing throws.
CAct — nothing is actually exercised.
DNothing is missing; a test that does not throw is a passing test.
Which assertions pass?
javascript
class Point { constructor(x) { this.x = x; } }
expect(new Point(1)).toEqual({ x: 1 }); // A
expect(new Point(1)).toStrictEqual({ x: 1 }); // B
expect({ a: 1, b: undefined }).toEqual({ a: 1 }); // C
expect({ a: 1, b: undefined }).toStrictEqual({ a: 1 }); // DAAll four.
BOnly A.
CB and D.
DA and C.
Why does the second test fail?
javascript
// counter.js
export let total = 0;
export const add = (n) => (total += n);
// counter.test.js
it('adds 5', () => { add(5); expect(total).toBe(5); });
it('adds 3', () => { add(3); expect(total).toBe(3); });AModule state persists across tests in the same file, so total is already 5 when the second test runs — it needs a reset in beforeEach.
BBecause the tests run in parallel and race each other.
CBecause let exports cannot be read from another module.
DBecause expect(total) captures the value before add runs.
Sign up free to play
Answer all 10 questions (7 more), see explanations for every answer, and track your score.