0
0
NextJSframework~20 mins

Testing API routes in NextJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
API Route Testing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Next.js API route test?
Given this Next.js API route test using the built-in test runner, what will be the test result?
NextJS
import { test, expect } from '@playwright/test';
import handler from '../../pages/api/hello';
import { createMocks } from 'node-mocks-http';

test('GET /api/hello returns status 200 and message', async () => {
  const { req, res } = createMocks({ method: 'GET' });
  await handler(req, res);
  expect(res._getStatusCode()).toBe(200);
  const data = JSON.parse(res._getData());
  expect(data.message).toBe('Hello from Next.js!');
});
AThe test fails because the handler function is not asynchronous.
BThe test passes because the API returns status 200 and the correct message.
CThe test fails because the response data is not JSON parsable.
DThe test fails because the API returns status 404 instead of 200.
Attempts:
2 left
💡 Hint
Check the status code and response message returned by the handler.
📝 Syntax
intermediate
2:00remaining
Which option correctly mocks a POST request to a Next.js API route?
You want to test a POST request with JSON body to a Next.js API route handler. Which code snippet correctly mocks the request and response?
Aconst { req, res } = createMocks({ method: 'POST', body: { name: 'Alice' } });
Bconst { req, res } = createMocks({ method: 'POST', json: { name: 'Alice' } });
Cconst { req, res } = createMocks({ method: 'POST', data: JSON.stringify({ name: 'Alice' }) });
Dconst { req, res } = createMocks({ method: 'POST', body: JSON.stringify({ name: 'Alice' }) });
Attempts:
2 left
💡 Hint
The body property should be a plain object, not a JSON string.
🔧 Debug
advanced
2:00remaining
Why does this Next.js API route test fail with TypeError?
Consider this test code snippet: import handler from '../../pages/api/user'; import { createMocks } from 'node-mocks-http'; test('GET /api/user returns user data', async () => { const { req, res } = createMocks({ method: 'GET' }); const result = await handler(req, res); expect(res._getStatusCode()).toBe(200); }); The test fails with TypeError: handler is not a function. Why?
AThe API route exports an object with multiple handlers, not a default function.
BThe createMocks function is not imported correctly.
CThe test file is missing the async keyword on the test function.
DThe handler function is asynchronous but called without await.
Attempts:
2 left
💡 Hint
Check how the API route exports its handler function.
state_output
advanced
2:00remaining
What is the response status after this Next.js API route test?
Given this API route handler and test, what status code will the test observe? // API route handler export default function handler(req, res) { if (req.method === 'POST') { res.status(201).json({ success: true }); } else { res.status(405).json({ error: 'Method Not Allowed' }); } } // Test snippet const { req, res } = createMocks({ method: 'GET' }); await handler(req, res); const status = res._getStatusCode();
A201
B200
C405
D500
Attempts:
2 left
💡 Hint
Check how the handler responds to GET requests.
🧠 Conceptual
expert
3:00remaining
Which testing approach best isolates Next.js API route logic from network dependencies?
You want to test your Next.js API route logic without making real HTTP requests or relying on external services. Which approach is best?
ADeploy the API route to a staging environment and test it with real network calls.
BUse a test HTTP client like axios to send requests to a running Next.js server instance.
CUse end-to-end testing tools like Playwright to test the API route through the browser.
DUse createMocks to mock req and res objects and call the handler function directly.
Attempts:
2 left
💡 Hint
Think about isolating the handler logic from network and external dependencies.