Mocks, Spies & Integration — Series 3

Preview — 3 of 10 questions

What do the three calls return?

javascript
const fn = vi.fn();
fn.mockReturnValue('default');
fn.mockReturnValueOnce('first').mockReturnValueOnce('second');

console.log(fn(), fn(), fn());
Adefault default default
Bfirst second undefined
Cfirst second default
Dsecond first default

Which setup lets you test the error branch of await api.fetchUser()?

Aapi.fetchUser.mockRejectedValue(new Error('network'))
Bapi.fetchUser.mockReturnValue(new Error('network'))
Capi.fetchUser.mockImplementation(() => { throw 'network'; }) with the caller unchanged
Dapi.fetchUser.mockResolvedValue(null)

The code calls track('purchase', { id: 7, ts: Date.now(), source: 'web' }). Which assertion is robust?

Aexpect(track).toHaveBeenCalledWith('purchase', { id: 7, ts: 1699999999999, source: 'web' })
Bexpect(track).toHaveBeenCalled()
Cexpect(track.mock.calls[0][1].id).toBe(7) and nothing else
Dexpect(track).toHaveBeenCalledWith('purchase', expect.objectContaining({ id: 7, source: 'web' }))

Sign up free to play

Answer all 10 questions (7 more), see explanations for every answer, and track your score.