Performance: Jest setup for Next.js
MEDIUM IMPACT
This setup affects the development environment speed and test execution time, indirectly impacting developer productivity and feedback loop speed.
import '@testing-library/jest-dom/extend-expect'; // jest.config.js with moduleNameMapper for CSS and images // Setup file to mock Next.js router // jest.config.js example: // module.exports = { // testEnvironment: 'jsdom', // moduleNameMapper: { // '^.+\\.(css|less|scss)$': 'identity-obj-proxy', // '^.+\\.(jpg|jpeg|png|svg)$': '<rootDir>/__mocks__/fileMock.js' // }, // setupFilesAfterEnv: ['<rootDir>/jest.setup.js'], // }; // jest.setup.js example: // import '@testing-library/jest-dom/extend-expect'; // import { useRouter } from 'next/router'; // jest.mock('next/router', () => ({ useRouter: jest.fn() })); import { render } from '@testing-library/react'; import Home from '../pages/index'; test('renders home page', () => { const { getByText } = render(<Home />); expect(getByText('Welcome')).toBeInTheDocument(); });
import '@testing-library/jest-dom/extend-expect'; // No jest.config.js or improper config // Tests run without mocking Next.js features // Example test import { render } from '@testing-library/react'; import Home from '../pages/index'; test('renders home page', () => { const { getByText } = render(<Home />); expect(getByText('Welcome')).toBeInTheDocument(); });
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| No Jest config for Next.js | N/A | N/A | N/A | [X] Bad |
| Proper Jest config with mocks | N/A | N/A | N/A | [OK] Good |