describe("<ComponentName />", () => {
beforeAll(() => { ... })
beforeEach(() =>
it("should ...", () => {
render(<Base {...Base.args}/>)
})
})
For some modules like next/router a default mock is better.
| Mock a function | const createCandyMock = jest.fn().mockReturnValue(value) |
| Mock a module |
import module from "importName"
|
| Mock function calls | createCandyMock.mock.calls each call is list of arguments |
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)
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() |
| Assert array or string length | expect(array).toHaveLength(number) |
| Assert types |
expect(any).toBeDefined()
|
| Array contains an item | expect(any).toContain(value) |
| Throws an error | expect(() => action).toThrow(error message) |
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 ...", () => ...) |