在Next.js上使用jest无法找到模块错误
您好,我遇到了一些麻烦,让Jest在我的NextJS应用程序上工作,当我使用脚本“ jest”时,我总是会遇到失败的执行,
FAIL __tests__/index.test.tsx
● Test suite failed to run
Cannot find module '@components/Layout' from 'pages/404.tsx'
Require stack:
pages/404.tsx
__tests__/index.test.tsx
> 1 | import { Layout } from '@components/Layout'
| ^
2 | import { ContainerChild } from '@components/Container'
3 | import { Button } from '@castia/components.ui.button'
4 | import Image from 'next/image'
at Resolver._throwModNotFoundError (node_modules/jest-resolve/build/resolver.js:491:11)
at Object.<anonymous> (pages/404.tsx:1:44)
Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 1.31 s
Ran all test suites.
这是我的测试文件
import renderer from "react-test-renderer";
import NotFound from "../pages/404";
describe("404 NotFound page", () => {
const tree = renderer.create(<NotFound />).toJSON();
expect(tree).toMatchSnapshot();
});
,这是我的组件文件,
import { Layout } from '@components/Layout'
import { ContainerChild } from '@components/Container'
import Image from 'next/image'
import styles from '@styles/404.module.scss'
const metaTags = {
title: 'Castia | 404',
description: 'No encontramos la página que buscabas',
}
export default function NotFoundPage() {
return (
<Layout metaTags={metaTags}>
<ContainerChild composition={'default'}>
<div className={styles.error404}>
<div>
<Image
src="/assets/image/error-404.png"
alt="No encontramos la página que buscabas"
width={475}
height={304}
/>
</div>
<h3>¡Ups! No encontramos la página que buscabas.</h3>
<p>
Revisa que hayas escrito correctamente el enlace o regresa a la
página principal.
</p>
<div className={styles.containerBtn}>
</div>
</div>
</ContainerChild>
</Layout>
)
}
我有jest.config.js文件像这样配置
const nextJest = require('next/jest')
const createJestConfig = nextJest({
dir: './',
})
const customJestConfig = {
setupFilesAfterEnv: ['<rootDir>/jest.setup.js'],
moduleNameMapper: {
'^@/components/(.*)$': '<rootDir>/components/$1',
'^@/pages/(.*)$': '<rootDir>/pages/$1',
'^@/ui/(.*)$': '<rootDir>/ui/$1',
'^@/api/(.*)$': '<rootDir>/api/$1',
'^@/components/(.*)$': '<rootDir>/components/$1',
'^@/context/(.*)$': '<rootDir>/context/$1',
'^@/styles/(.*)$': '<rootDir>/styles/$1',
'^@/lib/(.*)$': '<rootDir>/lib/$1',
'^@/hooks/(.*)$': '<rootDir>/hooks/$1',
'^@/utils/(.*)$': '<rootDir>/utils/$1',
},
testEnvironment: 'jest-environment-jsdom',
}
module.exports = createJestConfig(customJestConfig)
和我的tsconfig.json这样,
{
"compilerOptions": {
"target": "es6",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"incremental": true,
"baseUrl": ".",
"paths": {
"@ui/*": ["ui/*"],
"@api/*": ["pages/api/*"],
"@components/*": ["components/*"],
"@context/*": ["context/*"],
"@styles/*": ["styles/*"],
"@lib/*": ["lib/*"],
"@hooks/*": ["hooks/*"],
"@utils/*": ["utils/*"]
}
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"]
}
但我不知道为什么这会发生在我身上
Hello i have some troubles making jest work on my nextjs application, when i use the script "jest" i always get a failed execution and this result
FAIL __tests__/index.test.tsx
● Test suite failed to run
Cannot find module '@components/Layout' from 'pages/404.tsx'
Require stack:
pages/404.tsx
__tests__/index.test.tsx
> 1 | import { Layout } from '@components/Layout'
| ^
2 | import { ContainerChild } from '@components/Container'
3 | import { Button } from '@castia/components.ui.button'
4 | import Image from 'next/image'
at Resolver._throwModNotFoundError (node_modules/jest-resolve/build/resolver.js:491:11)
at Object.<anonymous> (pages/404.tsx:1:44)
Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 1.31 s
Ran all test suites.
this is my test file
import renderer from "react-test-renderer";
import NotFound from "../pages/404";
describe("404 NotFound page", () => {
const tree = renderer.create(<NotFound />).toJSON();
expect(tree).toMatchSnapshot();
});
and this is my component file
import { Layout } from '@components/Layout'
import { ContainerChild } from '@components/Container'
import Image from 'next/image'
import styles from '@styles/404.module.scss'
const metaTags = {
title: 'Castia | 404',
description: 'No encontramos la página que buscabas',
}
export default function NotFoundPage() {
return (
<Layout metaTags={metaTags}>
<ContainerChild composition={'default'}>
<div className={styles.error404}>
<div>
<Image
src="/assets/image/error-404.png"
alt="No encontramos la página que buscabas"
width={475}
height={304}
/>
</div>
<h3>¡Ups! No encontramos la página que buscabas.</h3>
<p>
Revisa que hayas escrito correctamente el enlace o regresa a la
página principal.
</p>
<div className={styles.containerBtn}>
</div>
</div>
</ContainerChild>
</Layout>
)
}
I have my jest.config.js file configured like this
const nextJest = require('next/jest')
const createJestConfig = nextJest({
dir: './',
})
const customJestConfig = {
setupFilesAfterEnv: ['<rootDir>/jest.setup.js'],
moduleNameMapper: {
'^@/components/(.*)
and my tsconfig.json like this
{
"compilerOptions": {
"target": "es6",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"incremental": true,
"baseUrl": ".",
"paths": {
"@ui/*": ["ui/*"],
"@api/*": ["pages/api/*"],
"@components/*": ["components/*"],
"@context/*": ["context/*"],
"@styles/*": ["styles/*"],
"@lib/*": ["lib/*"],
"@hooks/*": ["hooks/*"],
"@utils/*": ["utils/*"]
}
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"]
}
but i dont know why this is happening to me
: '<rootDir>/components/$1',
'^@/pages/(.*)
and my tsconfig.json like this
but i dont know why this is happening to me
: '<rootDir>/pages/$1',
'^@/ui/(.*)
and my tsconfig.json like this
but i dont know why this is happening to me
: '<rootDir>/ui/$1',
'^@/api/(.*)
and my tsconfig.json like this
but i dont know why this is happening to me
: '<rootDir>/api/$1',
'^@/components/(.*)
and my tsconfig.json like this
but i dont know why this is happening to me
: '<rootDir>/components/$1',
'^@/context/(.*)
and my tsconfig.json like this
but i dont know why this is happening to me
: '<rootDir>/context/$1',
'^@/styles/(.*)
and my tsconfig.json like this
but i dont know why this is happening to me
: '<rootDir>/styles/$1',
'^@/lib/(.*)
and my tsconfig.json like this
but i dont know why this is happening to me
: '<rootDir>/lib/$1',
'^@/hooks/(.*)
and my tsconfig.json like this
but i dont know why this is happening to me
: '<rootDir>/hooks/$1',
'^@/utils/(.*)
and my tsconfig.json like this
but i dont know why this is happening to me
: '<rootDir>/utils/$1',
},
testEnvironment: 'jest-environment-jsdom',
}
module.exports = createJestConfig(customJestConfig)
and my tsconfig.json like this
but i dont know why this is happening to me
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为组件文件中应该是“@/组件”
I think it should be "@/components" in your component file
看来您忘了在tsconfig.json中添加“页面”目录中的绝对路径映射。
尝试将其添加到您的tsconfig.json
It seems you forgot to add the absolute path mapping in your tsconfig.json for the 'pages' directory.
Try adding this to your tsconfig.json