0
0
NextJSframework~20 mins

Jest setup for Next.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Next.js Jest Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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?
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();
});
ATest fails with timeout error due to missing async handling.
BTest fails with error: Cannot find role 'heading'.
CTest passes because the heading with text 'Welcome to Next.js!' is found.
DTest fails because the component Home is not imported correctly.
Attempts:
2 left
💡 Hint
Check if the component renders a heading with the expected text and if the test queries it correctly.
📝 Syntax
intermediate
2: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?
Amodule.exports = { preset: 'next/jest', testEnvironment: 'jsdom' };
Bmodule.exports = { preset: 'ts-jest', testEnvironment: 'node' };
Cmodule.exports = { preset: 'next/babel', testEnvironment: 'jsdom' };
Dmodule.exports = { preset: 'react', testEnvironment: 'node' };
Attempts:
2 left
💡 Hint
Next.js provides a Jest preset to handle its features properly.
🔧 Debug
advanced
2: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 />);
});
AJest is not configured to transform ESModules and JSX, causing SyntaxError.
BThe Component import path is incorrect, causing a module not found error.
CThe render function is used incorrectly without a container.
DThe test is missing an assertion, causing Jest to fail.
Attempts:
2 left
💡 Hint
Check if Jest can understand JSX and ESModules without proper transformers.
state_output
advanced
2: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);
});
AError: useRouter.mockReturnValue is not a function
BCurrent path: /test-path
CCurrent path: /
DError: Cannot find text 'current path:'
Attempts:
2 left
💡 Hint
Mocking useRouter returns the pathname used by the component.
🧠 Conceptual
expert
3: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?
ABecause Jest only supports React apps created with Create React App and not Next.js.
BBecause Next.js uses non-standard JavaScript syntax that Jest does not support by default.
CBecause Jest cannot run tests on server-side code, so Next.js needs client-only mocks.
DBecause Next.js features require Jest to transform files and mock assets properly to avoid errors.
Attempts:
2 left
💡 Hint
Think about how Jest handles imports like CSS and images in Next.js.