Challenge - 5 Problems
Next.js Jest Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What will this Jest test output when run in a Next.js project?
Consider this simple React component and its Jest test in a Next.js environment.
What will the test output when executed?
What will the test output when executed?
NextJS
import { render, screen } from '@testing-library/react'; import Home from '../pages/index'; test('renders welcome message', () => { render(<Home />); const heading = screen.getByRole('heading', { name: /welcome to next\.js!/i }); expect(heading).toBeInTheDocument(); });
Attempts:
2 left
💡 Hint
Check if the component renders a heading with the expected text and if the test queries it correctly.
✗ Incorrect
The test renders the Home component and looks for a heading with the text 'Welcome to Next.js!'. If the component contains this heading, the test passes.
📝 Syntax
intermediate2:00remaining
Which Jest configuration snippet correctly sets up Next.js testing environment?
You want to configure Jest for a Next.js project. Which of these jest.config.js snippets is correct to support Next.js features?
Attempts:
2 left
💡 Hint
Next.js provides a Jest preset to handle its features properly.
✗ Incorrect
The correct preset for Jest in Next.js projects is 'next/jest' which configures Jest to work with Next.js features and uses 'jsdom' as the test environment for browser-like testing.
🔧 Debug
advanced2:00remaining
Why does this Jest test fail with a SyntaxError in a Next.js project?
Given this test file in a Next.js project, why does Jest throw a SyntaxError when running it?
NextJS
import { render } from '@testing-library/react'; import Component from '../components/Component'; test('renders component', () => { render(<Component />); });
Attempts:
2 left
💡 Hint
Check if Jest can understand JSX and ESModules without proper transformers.
✗ Incorrect
Jest needs to transform JSX and ESModules syntax. Without proper configuration (like using 'next/jest' preset), Jest throws SyntaxError on JSX code.
❓ state_output
advanced2:00remaining
What is the output of this Jest test with mocked Next.js router?
This test mocks Next.js useRouter hook. What will be the console output when running this test?
NextJS
import { render, screen } from '@testing-library/react'; import { useRouter } from 'next/router'; import Component from '../components/Component'; jest.mock('next/router', () => ({ useRouter: jest.fn(), })); test('shows current path', () => { useRouter.mockReturnValue({ pathname: '/test-path' }); render(<Component />); console.log(screen.getByText(/current path:/i).textContent); });
Attempts:
2 left
💡 Hint
Mocking useRouter returns the pathname used by the component.
✗ Incorrect
The mock sets useRouter to return pathname '/test-path'. The component renders text containing 'Current path: /test-path'. The console.log outputs this text.
🧠 Conceptual
expert3:00remaining
Which statement best explains why Jest needs a custom setup for Next.js projects?
Next.js uses features like dynamic imports, CSS modules, and image imports. Why does Jest require special configuration to test Next.js apps?
Attempts:
2 left
💡 Hint
Think about how Jest handles imports like CSS and images in Next.js.
✗ Incorrect
Next.js uses features like CSS modules and image imports that Jest cannot handle without transformation or mocks. The custom setup ensures Jest can process these files and avoid errors.