我已经对此做了大量研究,并找到了不少解决方案。我找到了一种感觉像是变通方法的东西,想让
不确定这是否是由于使用了
以下是我的
transform
和transformIgnorePatterns
工作起来。然而,似乎我唯一能够让其正常工作的事情就是在我的__mocks__
文件夹中手动添加一些模拟模块。不确定这是否是由于使用了
Nextjs
和Jest
造成的?以下是我的
jest.config.js
:const nextJest = require("next/jest");
const esModules = ["react-markdown", "rehype-raw", "remark-gfm"].join("|");
const createJestConfig = nextJest({
// Provide the path to your Next.js app to load next.config.js and .env files in your test environment
dir: "./",
});
// Add any custom config to be passed to Jest
const customJestConfig = {
setupFilesAfterEnv: ["<rootDir>/jest.setup.js"],
moduleNameMapper: {
// Handle module aliases (this will be automatically configured for you soon)
"^@/components/(.*)$": "<rootDir>/components/$1",
"^@/pages/(.*)$": "<rootDir>/pages/$1",
},
testEnvironment: "jest-environment-jsdom",
transform: {
[`(${esModules}).+\\.js$`]: "babel-jest",
},
transformIgnorePatterns: [
`[/\\\\]node_modules[/\\\\](?!${esModules}).+\\.(js|jsx|mjs|cjs|ts|tsx)$`,
],
};
// createJestConfig is exported this way to ensure that next/jest can load the Next.js config which is async
module.exports = createJestConfig(customJestConfig);
我读到需要一个 babel.config.js
文件。以下是该文件:
module.exports = {
presets: ["next/babel", "@babel/preset-env"],
};