Testing
Jest
Assertions, async tests and focused mock behavior.
Assertions
test('adds', () => { expect(1 + 2).toBe(3); });Define an isolated testexpect(result).toEqual({ id: 1 });Compare object structureexpect(items).toContain('Ada');Assert collection membershipexpect(() => fail()).toThrow();Assert a synchronous errorAsync and setup
await expect(load()).resolves.toEqual({ ok: true });Assert a fulfilled promiseawait expect(load()).rejects.toThrow('missing');Assert promise rejectionbeforeEach(() => resetState());Reset shared fixtures before each testafterEach(() => jest.restoreAllMocks());Restore spies and replaced propertiesMock and run
const save = jest.fn().mockResolvedValue({ id: 1 });Stub an asynchronous dependencyexpect(save).toHaveBeenCalledWith({ name: 'Ada' });Assert a meaningful call contractnpx jest --runInBandRun tests seriallynpx jest --coverageReport exercised source coverage