Jest

describe("<ComponentName />", () => {
  beforeAll(() => { ... })
  beforeEach(() => 
    it("should ...", () => {
      render(<Base {...Base.args}/>)
    })
  })

Mocking

For some modules like next/router a default mock is better.

Mock a function const createCandyMock = jest.fn().mockReturnValue(value)
const createCandyMock = jest.fn().mockImplementation(() => value)
const createCandyMock = jest.fn().mockResolvedValue(value)
Mock a module import module from "importName"
jest.mock("importName")
module.function.function mock
Mock function calls createCandyMock.mock.calls each call is list of arguments

Mocking API calls

Install jest-fetch-mock.

fetchMock.mockOnce(JSON.stringify(data), { status: status code})
const [url, request] = fetchMock.mock.calls[index]
const method = request?.method
const body = JSON.parse(request?.body)

Assertions

A negation modifier not can be used.

Array contains a subset of elements expect(array).arrayContaining(subarray)
Object contains subset of properties expect(object).objectContaining(subobject)
Assert resolved promises return expect(promise).resolves.assertion
Assert rejected promises return expect(promise).rejects.assertion
Exact value expect(any).toEqual(value)
A mock function has been called expect(mockFn).toHaveBeenCalled()
expect(mockFn).toHaveBeenCalledTimes(number)
expect(mockFn).toHaveBeenCalledWith(arg1, arg2, arg3)
Assert array or string length expect(array).toHaveLength(number)
Assert types expect(any).toBeDefined()
expect(any).toBeFalsy()
expect(any).toBeNull()
expect(any).toBeTruthy()
expect(any).toBeUndefined()
expect(any).toBeNaN()
expect(any).toBeInstanceof()
Array contains an item expect(any).toContain(value)
Throws an error expect(() => action).toThrow(error message)

Execution Modifiers

Avoid debug modifiers skip, and only.

Todo, for planned tests it.todo("should ...")
Run for dataset it.each([data])("should ...", (args) => ...)
Run tests concurrently it.concurrent("should ...", () => ...)