0
0
NextJSframework~20 mins

Mocking data fetching in NextJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Mocking Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What will this Next.js component render when data fetching is mocked?

Consider this Next.js component that fetches user data using a mocked fetch function. What will it render?

NextJS
import React, { useEffect, useState } from 'react';

const mockFetch = () => Promise.resolve({ json: () => Promise.resolve({ name: 'Alice' }) });

export default function User() {
  const [user, setUser] = useState(null);

  useEffect(() => {
    mockFetch().then(res => res.json()).then(data => setUser(data));
  }, []);

  if (!user) return <p>Loading...</p>;
  return <p>User: {user.name}</p>;
}
A<p>Loading...</p> initially, then <p>User: Alice</p>
B<p>User: Alice</p> immediately without loading state
CComponent throws an error because mockFetch is not defined
D<p>Loading...</p> forever, never showing user name
Attempts:
2 left
💡 Hint

Think about how useEffect and state updates work asynchronously.

📝 Syntax
intermediate
2:00remaining
Which option correctly mocks fetch in a Next.js test?

You want to mock the global fetch function in a Next.js test to return JSON data { message: 'Hello' }. Which option is correct?

Aglobal.fetch = () => ({ json: () => ({ message: 'Hello' }) });
Bglobal.fetch = jest.fn(() => ({ json: () => Promise.resolve({ message: 'Hello' }) }));
Cglobal.fetch = jest.mock(() => Promise.resolve({ message: 'Hello' }));
Dglobal.fetch = jest.fn(() => Promise.resolve({ json: () => Promise.resolve({ message: 'Hello' }) }));
Attempts:
2 left
💡 Hint

Remember fetch returns a promise that resolves to a response object with a json() method that returns a promise.

🔧 Debug
advanced
2:00remaining
Why does this mocked fetch cause a runtime error in Next.js?

Given this mocked fetch in a Next.js component test, why does it cause a runtime error?

NextJS
global.fetch = jest.fn(() => ({ json: () => ({ data: 'test' }) }));
Afetch must return a promise, but this returns an object, causing a runtime error
Bjson method must return an object, but this returns a promise, causing a runtime error
Cjest.fn cannot be used to mock fetch globally
DThe fetch mock is missing a call to next() causing infinite loop
Attempts:
2 left
💡 Hint

Recall how the real fetch API behaves with promises.

state_output
advanced
2:00remaining
What is the final state of data after this mocked fetch in Next.js?

In this Next.js component, what will be the final value of data after the mocked fetch resolves?

NextJS
import { useState, useEffect } from 'react';

const mockFetch = () => Promise.resolve({ json: () => Promise.resolve({ items: [1, 2, 3] }) });

export default function List() {
  const [data, setData] = useState([]);

  useEffect(() => {
    mockFetch()
      .then(res => res.json())
      .then(json => setData(json.items));
  }, []);

  return null;
}
A[{ items: [1, 2, 3] }]
B[] (empty array)
C[1, 2, 3]
Dundefined
Attempts:
2 left
💡 Hint

Look at what setData is called with after the fetch.

🧠 Conceptual
expert
2:00remaining
Which statement best explains why mocking fetch is important in Next.js testing?

Why do developers mock the fetch API when testing Next.js components that fetch data?

ABecause Next.js does not support real fetch calls during tests
BTo isolate component logic from network variability and ensure tests run fast and reliably
CTo avoid writing asynchronous code in tests
DBecause mocking fetch automatically improves component performance in production
Attempts:
2 left
💡 Hint

Think about what can make tests flaky or slow when they depend on real network calls.