Skip to content

Commit e903833

Browse files
feat(posts): add "How to fix Next.js import Jest error"
Post: 2025-08-14-fix-next-import-jest-error.md
1 parent 7cb4459 commit e903833

1 file changed

Lines changed: 40 additions & 0 deletions

File tree

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
---
2+
layout: post
3+
title: How to fix Next.js import Jest error
4+
date: 2025-08-14 22:14:29
5+
excerpt: How to fix Next.js import Jest error.
6+
categories: next jest typescript test
7+
---
8+
9+
## Problem
10+
11+
If you've updated [Next.js](https://nextjs.org/) and got the Jest test error:
12+
13+
```sh
14+
npm test
15+
```
16+
17+
```
18+
Error: Jest: Failed to parse the TypeScript config file /home/runner/work/nextjs-typescript-template/nextjs-typescript-template/jest.config.ts
19+
Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/home/runner/work/nextjs-typescript-template/nextjs-typescript-template/node_modules/next/jest' imported from /home/runner/work/nextjs-typescript-template/nextjs-typescript-template/jest.config.ts
20+
Did you mean to import "next/jest.js"?
21+
at readConfigFileAndSetRootDir (/home/runner/work/nextjs-typescript-template/nextjs-typescript-template/node_modules/jest-config/build/index.js:2269:13)
22+
at async readInitialOptions (/home/runner/work/nextjs-typescript-template/nextjs-typescript-template/node_modules/jest-config/build/index.js:1147:13)
23+
at async readConfig (/home/runner/work/nextjs-typescript-template/nextjs-typescript-template/node_modules/jest-config/build/index.js:918:7)
24+
at async readConfigs (/home/runner/work/nextjs-typescript-template/nextjs-typescript-template/node_modules/jest-config/build/index.js:1168:26)
25+
at async runCLI (/home/runner/work/nextjs-typescript-template/nextjs-typescript-template/node_modules/@jest/core/build/index.js:1393:7)
26+
at async Object.run (/home/runner/work/nextjs-typescript-template/nextjs-typescript-template/node_modules/jest-cli/build/index.js:656:9)
27+
```
28+
29+
## Solution
30+
31+
The fix is that you need to replace `next/jest` with `next/jest.js` if you're using [TypeScript](https://nextjs.org/docs/app/guides/testing/jest#manual-setup):
32+
33+
```diff
34+
// jest.config.ts
35+
import type { Config } from 'jest'
36+
-import nextJest from 'next/jest'
37+
+import nextJest from 'next/jest.js'
38+
```
39+
40+
See [example](https://github.com/remarkablemark/nextjs-typescript-template/commit/8faa647090427fcf7bc5ae4736b97b1fef1f1ab2).

0 commit comments

Comments
 (0)