Challenge - 5 Problems
API Route Testing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2: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!'); });
Attempts:
2 left
💡 Hint
Check the status code and response message returned by the handler.
✗ Incorrect
The handler returns status 200 with a JSON message 'Hello from Next.js!'. The test correctly mocks the request and response, so it passes.
📝 Syntax
intermediate2: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?
Attempts:
2 left
💡 Hint
The body property should be a plain object, not a JSON string.
✗ Incorrect
The createMocks function expects the body as a plain object for JSON requests. Passing JSON string or other keys like 'json' or 'data' will not work.
🔧 Debug
advanced2: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?
Attempts:
2 left
💡 Hint
Check how the API route exports its handler function.
✗ Incorrect
If the API route exports multiple named handlers or an object, importing default will not give a function, causing TypeError.
❓ state_output
advanced2: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();
Attempts:
2 left
💡 Hint
Check how the handler responds to GET requests.
✗ Incorrect
The handler returns 405 for any method other than POST. Since the test uses GET, status is 405.
🧠 Conceptual
expert3: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?
Attempts:
2 left
💡 Hint
Think about isolating the handler logic from network and external dependencies.
✗ Incorrect
Mocking req and res with createMocks lets you test the handler function directly, isolating logic and avoiding network overhead.