Next.js Testing a Next.js App
Tests check the small parts you can run. A page that needs the server is checked in the browser instead.
The search test that passes
The test calls the plain function, not the page. Four checks pass.
it('finds Dune when the query is dune', () => {
const results = searchBooks('dune');
expect(results).toHaveLength(1);
expect(results[0]?.title).toBe('Dune');
});The error the page test gives
The page test tried to render the async component. Vitest printed this error, then timed out.
<Results> is an async Client Component. Only Server Components can be async at the moment.Example
npm testKey points
- Vitest runs the tests. jsdom draws the page in memory.
- The BookCard test renders the card and reads its text. Two tests pass.
- searchBooks is a plain function. Its test runs fast, and four tests pass.
- The page test timed out after 5000ms. The page is an async Server Component, and jsdom cannot render it.
- The error names the cause: "<Results> is an async Client Component. Only Server Components can be async at the moment."
- The search page is checked with curl against the running server instead.
In this example, one command runs all the tests. Two files pass, and six tests pass. Four tests check the search function, and two tests check the BookCard. The page test is gone. It timed out because jsdom cannot render an async Server Component. The page itself is checked with curl, not with Vitest.
Note
Test the logic, not the framework. The framework is already tested by its own team.
Try it Yourself
Edit the code, then press Run.
Running is simulated · TypeScript Playground
Exercise
Why did the page-level search test time out?