contentintech
Testing

Jest

Assertions, async tests and focused mock behavior.

Assertions

test('adds', () => { expect(1 + 2).toBe(3); });Define an isolated test
expect(result).toEqual({ id: 1 });Compare object structure
expect(items).toContain('Ada');Assert collection membership
expect(() => fail()).toThrow();Assert a synchronous error

Async and setup

await expect(load()).resolves.toEqual({ ok: true });Assert a fulfilled promise
await expect(load()).rejects.toThrow('missing');Assert promise rejection
beforeEach(() => resetState());Reset shared fixtures before each test
afterEach(() => jest.restoreAllMocks());Restore spies and replaced properties

Mock and run

const save = jest.fn().mockResolvedValue({ id: 1 });Stub an asynchronous dependency
expect(save).toHaveBeenCalledWith({ name: 'Ada' });Assert a meaningful call contract
npx jest --runInBandRun tests serially
npx jest --coverageReport exercised source coverage

Section navigation