Complete the code to import the testing library in a Next.js test file.
import { render, screen } from '[1]';
We use @testing-library/react to render components and query the DOM in Next.js tests.
Complete the Jest configuration file to specify the test environment for Next.js.
module.exports = {
testEnvironment: '[1]'
};Jest uses jsdom as the test environment to simulate a browser for Next.js component tests.
Fix the error in the Jest setup file to properly import the Next.js testing utilities.
import '[1]';
The correct import for Next.js Jest setup is next/jest.
Fill both blanks to create a Jest config that uses Next.js custom Jest config and sets up the test environment.
const nextJest = require('[1]'); const createJestConfig = nextJest({ dir: './', }); const customJestConfig = { testEnvironment: '[2]', }; module.exports = createJestConfig(customJestConfig);
We require next/jest to create the Jest config and set jsdom as the test environment for browser-like testing.
Fill all three blanks to write a simple test that renders a Next.js component and checks for text content.
import { render, screen } from '[1]'; import Home from '[2]'; test('renders welcome message', () => { render(<Home />); const text = screen.getByText('[3]'); expect(text).toBeInTheDocument(); });
We import @testing-library/react to render and query. The Home component is from pages/index. The test looks for the welcome text.