简体   繁体   English

如何使用 SWC、Jest、Eslint 和 Typescript 正确配置 NextJS 12.2?

[英]How to properly configure NextJS 12.2 w/ SWC, Jest, Eslint, and Typescript?

Using VSCode and can't resolve error that next/babel with Jest files.使用 VSCode 并且无法解决带有 Jest 文件的 next/babel 错误。 Any Suggestions?有什么建议么?

I am using NextJS with SWC and have "extends": "next" in my.eslintrc file.我将 NextJS 与 SWC 一起使用,并且在 my.eslintrc 文件中有 "extends": "next" 。

Parsing error: Cannot find module 'next/babel'解析错误:找不到模块“next/babel”

This below is from my package.json file.下面是我的 package.json 文件。

{
...
  "dependencies": {
    "@emotion/cache": "^11.9.3",
    "@emotion/react": "^11.9.3",
    "@emotion/server": "^11.4.0",
    "@emotion/styled": "^11.9.3",
    "@mui/icons-material": "^5.8.4",
    "@mui/material": "^5.9.2",
    "next": "12.2.3",
    "react": "18.2.0",
    "react-dom": "18.2.0",
  },
  "devDependencies": {
    "@swc/core": "^1.2.220",
    "@swc/jest": "^0.2.22",
    "@testing-library/dom": "^8.16.0",
    "@testing-library/jest-dom": "^5.16.2",
    "@testing-library/react": "^13.3.0",
    "@testing-library/user-event": "14.3.0",
    "@types/node": "^18.6.3",
    "@types/react": "18.0.15",
    "@types/testing-library__jest-dom": "^5.14.5",
    "eslint": "8.20.0",
    "eslint-plugin-testing-library": "^5.6.0",
    "jest": "^28.1.3",
    "jest-environment-jsdom": "^28.1.3",
    "next-transpile-modules": "9.0.0",
    "typescript": "^4.6.2"
}

Issue was being caused by file type/ extension behavior.问题是由文件类型/扩展名行为引起的。 Using Module.Exports with a.eslintrc.js file was causing the issue.将 Module.Exports 与 a.eslintrc.js 文件一起使用会导致问题。 Converting to.eslintrc in json format resolves the error.转换为 json 格式的 .eslintrc 可以解决该错误。

// .eslintrc.js - Does not work

// Commented for testing
// do this module.exports = require("baseconfig/eslint-preset"); 

module.exports = {
    extends: ["prettier", "next/core-web-vitals"],
    plugins: ["testing-library"],
    settings: {
      next: {
        rootDir: ["apps/*/", "packages/*/"],
      },
    },
    rules: {
      "@next/next/no-html-link-for-pages": "off",
    },
    overrides: [
      // Only uses Testing Library lint rules in test files
      {
        "files": [
          "**/__tests__/**/*.[jt]s?(x)",
          "**/?(*.)+(spec|test).[jt]s?(x)"
        ],
        "extends": ["plugin:testing-library/react"]
      }
    ]
  };
// .eslintrc - works
{
    "extends": ["prettier", "next/core-web-vitals"],
    "plugins": ["testing-library"],
    "settings": {
      "next": {
        "rootDir": ["apps/*/", "packages/*/"],
      },
    },
    "rules": {
      "@next/next/no-html-link-for-pages": "off",
    },
    "overrides": [
      // Only uses Testing Library lint rules in test files
      {
        "files": [
          "**/__tests__/**/*.[jt]s?(x)",
          "**/?(*.)+(spec|test).[jt]s?(x)"
        ],
        "extends": ["plugin:testing-library/react"]
      }
    ]
  }

Did you create a jest.config.js?你创建了一个 jest.config.js 吗? This is from the official Next.js docs https://nextjs.org/docs/testing#jest-and-react-testing-library这是来自官方 Next.js 文档https://nextjs.org/docs/testing#jest-and-react-testing-library

  transform: {
    '\\.[jt]sx?$': ['babel-jest', { presets: ['next/babel'] }],
  },

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

相关问题 如何配置@typescript-eslint 规则 - How to configure @typescript-eslint rules 如何使用 SWC 生成 TypeScript 代码 - How to generating TypeScript code with SWC 如何在玩笑中允许 scss 进行打字稿 nextjs 的单元测试? - How to allow scss in jest for unit testing for typescript nextjs? 如何使用 ESLint + Prettier + Airbnb 规则 + TypeScript + Vetur 配置 Vue CLI 4? - How to configure Vue CLI 4 with ESLint + Prettier + Airbnb rules + TypeScript + Vetur? 配置 Jest 以支持 Typescript (NodeJs) - configure Jest to support Typescript (NodeJs) 开玩笑配置没有Webpack的打字稿 - Jest configure typescript without webpack 如何检测 NextJS 项目中的所有 Typescript 和 ESLint 错误,而不仅仅是在打开的文件中? - How to detect all Typescript and ESLint errors in NextJS project, not just in opened files? 如何使用 Jest 和 TypeScript 正确存根/模拟 AWS SecretManager - how to properly stub/mock AWS SecretManager with Jest and TypeScript 如何将 typescript-eslint 规则添加到 eslint - How to add typescript-eslint rules to eslint 如何在 typescript-node 项目中配置 jest-mongodb(@shelf/jest-mongodb)? - How to configure jest-mongodb(@shelf/jest-mongodb) in typescript-node project?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM