简体   繁体   English

在 TypeScript 中使用 NextJS 设置 Jest + React 测试库 — 设置 upp jest.config.js 时出错

[英]Setup Jest + React testing library using NextJS in TypeScript — error setting upp jest.config.js

I am trying to set up Jest with a NextJS application, currently in jest.config.js :我正在尝试使用 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"
    }
  };

In my setupTests.tsx I have:在我的setupTests.tsx我有:

import "@testing-library/jest-dom/extend-expect";

However when I run npm run test I get the following error:但是,当我运行npm run test时,我收到以下错误:

  Module identity-obj-proxy in the transform option was not found.
         <rootDir> is: ....

If I remove the line in jest.config.js I get:如果我删除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";

What configuration should I use for the css and scss files? cssscss文件应该使用什么配置?

** EDIT ** ** 编辑 **

I made sure that identity-obj-proxy is in my devDependencies however, now I get an error:我确保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";

You need to install identity-obj-proxy.您需要安装 identity-obj-proxy。 Run >>> yarn add -D identity-obj-proxy运行 >>> yarn add -D identity-obj-proxy

Try this:尝试这个:

  1. Create a file style-mock.js with this content使用此内容创建文件style-mock.js
module.exports = {};
  1. Specify it inside the moduleNameMapper field of jest config在 jest config 的moduleNameMapper字段中指定它
moduleNameMapper: {
  '^.+\\.(css|less)$': '<rootDir>/jest-config/style-mock.js'
}

With this setup, it will mock all css import, hope it helps通过此设置,它将模拟所有 css 导入,希望对您有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 create-react-app 中使用 jest.config.js - How to use jest.config.js with create-react-app 带有 Mono Repo 的 react-scripts 的无效 testPattern --config=jest.config.js - Invalid testPattern --config=jest.config.js with react-scripts with Mono Repo Jest &amp; React &amp; Typescript &amp; React-Testing-Library 错误 - Jest & React & Typescript & React-Testing-Library error 在 jest.config.js 文件中导入本地文件 - Importing local files in `jest.config.js` file jest.config.js 中的`moduleNameMapper` 设置不适用于 CircleCI - `moduleNameMapper` settings in jest.config.js doesn't work on CircleCI 如何使用不同的 jest.config.js 进行单元和组件测试? - How to use different jest.config.js for unit and component tests? jest.config.js 失败 - 意外的标记“。” 在 module.exports 中 - jest.config.js fails - unexpected token '.' in module.exports 使用 Jest 和 @testing-library/react-hooks 在 TypeScript 中单元测试自定义 React Hook - Unit Testing Custom React Hooks in TypeScript using Jest and @testing-library/react-hooks 如何使用 React 测试库 + Jest 模拟测试函数 - How to mock functions for testing using the React Testing Library + Jest 使用 react 测试库和 jest 进行 antd 组件测试 - antd component testing using react testing library and jest
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM