gpt4 book ai didi

javascript - 使用 TypeScript 中的 Next JS 设置 Jest + React 测试库——设置 jest.config.js 时出错

转载 作者:行者123 更新时间:2023-12-04 07:54:34 31 4
gpt4 key购买 nike

我正在尝试使用 NextJS 应用程序设置 Jest,目前在 jest.config.js :

module.exports = {
testPathIgnorePatterns: ["<rootDir>/.next/", "node_modules/"],
setupFilesAfterEnv: ["<rootDir>/__tests__/setupTests.tsx"],
transform: {
"^.+\\.(js|jsx|ts|tsx)$": "<rootDir>/node_modules/babel-jest",
"\\.(css|less|scss|sass)$": "identity-obj-proxy"
}
};
在我的 setupTests.tsx我有:
import "@testing-library/jest-dom/extend-expect";
但是,当我运行时 npm run test我收到以下错误:
  Module identity-obj-proxy in the transform option was not found.
<rootDir> is: ....
如果我删除 jest.config.js 中的行我得到:
Test suite failed to run

Jest encountered an unexpected token

This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.

By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".

globals.css:2
body {
^

SyntaxError: Unexpected token '{'

> 5 | import "../styles/globals.css";
我应该为 css 使用什么配置和 scss文件?
** 编辑 **
我确保 identity-obj-proxy但是,在我的 devDependencies 中,现在出现错误:
Test suite failed to run

Test suite failed to run

TypeError: Jest: a transform must export a `process` function.

> 5 | import "../styles/globals.css";
| ^
6 | import React from "react";

最佳答案

您需要安装 identity-obj-proxy。运行 >>> yarn add -D identity-obj-proxy

关于javascript - 使用 TypeScript 中的 Next JS 设置 Jest + React 测试库——设置 jest.config.js 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66769400/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com