Skip to content
Chapter 87Lesson 7

Quiz - Unit tests for lib

Quiz progress

0 / 0

A /lib function refundCharge is broken — it resolves false for every charge. This test reports a pass under Vitest 3:

it('refunds a settled charge', async () => {
expect(refundCharge(charge)).resolves.toBe(true);
});

Why does the broken code slip through green?

The .resolves assertion builds a promise that the it callback never awaits, so the callback finishes cleanly and the runner records a pass before the rejection lands.
.resolves only works on functions that throw, so it silently skips any promise that resolves to a boolean.
The callback is marked async, which tells Vitest to ignore the assertions inside it.

A date codec hands back a Temporal.PlainDate. Which assertion actually pins the calendar day the test cares about?

expect(due.toString()).toBe('2026-03-15');
expect(due).toEqual(Temporal.PlainDate.from('2026-03-15'));
expect(due).toBe(Temporal.PlainDate.from('2026-03-15'));

A test calls vi.useFakeTimers() and vi.setSystemTime(new Date('2026-01-15T12:00:00Z')), then asserts that Temporal.Now.instant() reports that frozen moment. The assertion fails and reads the real wall-clock time. Why?

vi.useFakeTimers() patches Date, setTimeout, and friends, but not Temporal.Now — Temporal’s clock is a separate mechanism the fakes never reach.
vi.setSystemTime only takes effect after the first vi.advanceTimersByTime call.
Fake timers were installed in beforeEach instead of beforeAll, so they hadn’t taken effect yet.

You want a type test that catches it when someone adds a fourth cancelled member to the InvoiceState discriminated union. Which matcher fails loudly on the widening?

expectTypeOf<InvoiceState>().toEqualTypeOf<Draft | Sent | Paid>();
expectTypeOf<InvoiceState>().toExtend<Draft | Sent | Paid>();

Sort these test-data needs by the tool you’d reach for: a single paid invoice for one test’s assertion, a captured Stripe checkout.session.completed payload for signature verification, and 50 invoices to fill the dev list page. Which mapping is right?

Single invoice → factory; captured Stripe payload → static fixture; 50 invoices → seed.
Single invoice → fixture; captured Stripe payload → factory; 50 invoices → seed.
Single invoice → factory; captured Stripe payload → seed; 50 invoices → fixture.

You’re testing that a /lib function returns Result.err with a particular code. Which assertion is the durable one, and why?

expect(result).toMatchObject({ ok: false, error: { code: 'not_found' } });

Partial match pins the discriminator and the code — the contract — and stays quiet about userMessage and any field the error grows later.

expect(result).toEqual({
ok: false,
error: { code: 'not_found', userMessage: 'Invoice not found.' },
});

Exhaustive match proves the whole shape, so nothing can drift unnoticed.

Quiz complete

Score by topic