All quizzesEasy
Unit Testing Basics — Series 2
Preview — 3 of 10 questions
What does expect.objectContaining add here?
javascript
await service.create({ name: 'Bombay' });
expect(repo.save).toHaveBeenCalledTimes(1);
expect(repo.save).toHaveBeenCalledWith(
expect.objectContaining({ name: 'Bombay' }),
);AIt converts the assertion into a snapshot comparison
BIt asserts that the saved object has exactly the listed properties and no others
CIt matches an argument that includes the listed properties, ignoring any extra ones — so the test does not break when an unrelated field such as createdAt or an id is added later
DIt deep-clones the argument before comparing, avoiding mutation issues
What does this do, and how does it differ from resetAllMocks()?
javascript
beforeEach(() => {
jest.clearAllMocks();
});AclearAllMocks wipes recorded calls and instances but keeps any implementation the mock was given; resetAllMocks also removes the implementation, so a mockResolvedValue set in the suite's setup would be lost
BThey are identical; clear is the older alias
CclearAllMocks removes the mocks entirely, so subsequent calls hit the real implementation
DclearAllMocks applies only to spies, and resetAllMocks only to jest.fn() mocks
What convention is this test following?
javascript
it('marks the order as paid once payment succeeds', async () => {
const order = buildOrder({ status: 'pending' });
repo.findOneBy.mockResolvedValue(order);
payments.charge.mockResolvedValue({ ok: true });
await service.pay(order.id);
expect(repo.save).toHaveBeenCalledWith(expect.objectContaining({ status: 'paid' }));
});ARed-green-refactor, applied within a single test body
BGiven-when-then, which requires a BDD framework such as Cucumber
CPage-object, adapted from browser testing
DArrange-act-assert: set up the state and doubles, perform exactly one action, then assert the outcome — which keeps each test about one behaviour and makes a failure immediately legible
Sign up free to play
Answer all 10 questions (7 more), see explanations for every answer, and track your score.