Best practices for writing JavaScript/TypeScript tests using Jest, including mocking strategies, test structure, and common patterns.
.test.ts or .test.js suffix__tests__ directorydescribe('Component/Function/Class', () => { it('should do something', () => {}) })jest.mock() for module-level mocksjest.spyOn() for specific function mocksmockImplementation() or mockReturnValue() to define mock behaviorjest.resetAllMocks() in afterEachresolves/rejects matchers for promisesjest.setTimeout()userEvent over fireEvent for more realistic user interactionsexpect(value).toBe(expected), expect(value).toEqual(expected)expect(value).toBeTruthy(), expect(value).toBeFalsy()expect(value).toBeGreaterThan(3), expect(value).toBeLessThanOrEqual(3)expect(value).toMatch(/pattern/), expect(value).toContain('substring')expect(array).toContain(item), expect(array).toHaveLength(3)expect(object).toHaveProperty('key', value)expect(fn).toThrow(), expect(fn).toThrow(Error)expect(mockFn).toHaveBeenCalled(), expect(mockFn).toHaveBeenCalledWith(arg1, arg2)