运行Next.js构建时未定义玩笑参考错误'描述'

时间:2020-05-24 17:28:32

标签: reactjs typescript jestjs next.js ts-jest

我正在努力完成next build而不会引发以下错误:

> Build error occurred
{ ReferenceError: describe is not defined

dev服务器和测试运行正常,但似乎无法在我的jest文件中使用describe全局*.spec.tsx

我将节点版本10.16.1与以下package.json一起使用:

{
    "name": "trufflus",
    "version": "0.1.0",
    "private": true,
    "scripts": {
        "dev": "next dev",
        "build": "next build",
        "start": "next start",
        "deploy": "npm run build && next export",
        "test": "jest",
        "test:watch": "yarn test -- --watch",
        "lint": "eslint '*/**/*.{js,ts,tsx}' --quiet --fix"
    },
    "husky": {
        "hooks": {
            "pre-commit": "lint-staged"
        }
    },
    "lint-staged": {
        "*.{js,ts,tsx}": [
            "eslint --fix"
        ]
    },
    "dependencies": {
        "next": "9.4.2",
        "react": "16.13.1",
        "react-dom": "16.13.1",
        "tailwindcss": "^1.4.6",
        "typeface-lobster": "^0.0.72",
        "typeface-roboto": "^0.0.75"
    },
    "devDependencies": {
        "autoprefixer": "^9.8.0",
        "postcss-import": "^12.0.1",
        "postcss-preset-env": "^6.7.0",
        "@testing-library/jest-dom": "^5.8.0",
        "@testing-library/react": "^10.0.4",
        "@types/jest": "^25.2.3",
        "@types/node": "^14.0.5",
        "@types/react": "^16.9.35",
        "@typescript-eslint/eslint-plugin": "^3.0.0",
        "@typescript-eslint/parser": "^3.0.0",
        "eslint": "^7.1.0",
        "eslint-config-prettier": "^6.11.0",
        "eslint-plugin-jest": "^23.13.1",
        "eslint-plugin-prettier": "^3.1.3",
        "eslint-plugin-react": "^7.20.0",
        "husky": "^4.2.5",
        "jest": "25.5.4",
        "lint-staged": "^10.2.6",
        "prettier": "^2.0.5",
        "prop-types": "^15.7.2",
        "ts-jest": "25.5.1",
        "typescript": "^3.9.3"
    }
}

我有两个单独的TypeScript配置文件;一种用于开发/生产产品:

{
    "compilerOptions": {
        "target": "es5",
        "lib": ["dom", "dom.iterable", "esnext"],
        "allowJs": true,
        "skipLibCheck": true,
        "strict": false,
        "forceConsistentCasingInFileNames": true,
        "noEmit": true,
        "esModuleInterop": true,
        "module": "esnext",
        "moduleResolution": "node",
        "resolveJsonModule": true,
        "isolatedModules": true,
        "jsx": "preserve"
    },
    "exclude": ["node_modules"],
    "include": ["next-env.d.ts", "node_modules/@types", "**/*.ts", "**/*.tsx"]

还有一个用于测试:

{
    "extends": "./tsconfig.json",
    "compilerOptions": {
        "jsx": "react",
        "types": ["jest", "node"]
    },
    "exclude": ["**/*.spec.tsx"]
}

我有单独的componentspages文件夹,它们都包含*.spec.tsx个文件。

我已尝试升级Node版本,并根据此问题进行操作:

ReferenceError: describe is not defined in Jest + Typescript

我上面的配置应该可以工作,但是我不知道现在哪里失败了。

我尝试将jestts-jestnode升级到无济于事!

1 个答案:

答案 0 :(得分:1)

开发服务器和测试运行正常,但似乎并没有在我的* .spec.tsx文件中引起不必要的全局描述

这可能是因为您用于测试的tsconfig包含"exclude": ["**/*.spec.tsx"]行,而您的dev / prod tsconfig却没有。

您需要在构建过程中将测试文件排除在编译范围之外。