CRA 忽略 jest.config 文件

Posted

技术标签:

【中文标题】CRA 忽略 jest.config 文件【英文标题】:CRA Ignoring jest.config file 【发布时间】:2021-06-04 20:33:06 【问题描述】:

我正在使用带有 typescript 和酶的 react 16,但 jest 没有读取我的适配器所在的 setupTests.ts 文件。如果我尝试在所有测试开始时配置适配器,它可以工作,但是当我尝试这样做时,它会给我 Enzyme 期望配置适配器错误。使用打字稿时我需要一些特定的配置吗?

package.json


  "name": "web-to-do",
  "version": "0.1.0",
  "private": true,
  "homepage": "https://lucasgseabra.github.io/to-do-web/",
  "dependencies": 
    "@fortawesome/fontawesome-svg-core": "^1.2.32",
    "@fortawesome/free-solid-svg-icons": "^5.15.1",
    "@fortawesome/react-fontawesome": "^0.1.14",
    "@reduxjs/toolkit": "^1.5.0",
    "@types/jest": "^26.0.20",
    "@types/node": "^14.14.31",
    "@types/react": "^17.0.2",
    "@types/react-dom": "^17.0.1",
    "axios": "^0.20.0",
    "bootstrap": "^4.5.3",
    "font-awesome": "^4.7.0",
    "react": "^16.14.0",
    "react-bootstrap": "^1.3.0",
    "react-dom": "^16.14.0",
    "react-redux": "^7.2.2",
    "react-router-dom": "^5.2.0",
    "react-scripts": "3.4.3",
    "ts-jest": "^26.5.3",
    "typescript": "^4.2.2"
  ,
  "scripts": 
    "start": "react-scripts start",
    "build": "react-scripts build",
    "lint": "./node_modules/.bin/eslint src/*.tsx",
    "test": "react-scripts test",
    "test:watch": "jest --watch",
    "eject": "react-scripts eject",
    "predeploy": "yarn build",
    "deploy": "gh-pages -d build"
  ,
  "browserslist": 
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  ,
  "devDependencies": 
    "@types/enzyme": "^3.10.8",
    "@types/enzyme-adapter-react-16": "^1.0.6",
    "@types/react-bootstrap": "^0.32.25",
    "@types/react-redux": "^7.1.16",
    "@types/react-router": "^5.1.12",
    "@types/react-router-dom": "^5.1.7",
    "@types/react-test-renderer": "^17.0.1",
    "@types/redux-promise": "^0.5.29",
    "@types/redux-thunk": "^2.1.0",
    "@typescript-eslint/eslint-plugin": "^4.16.1",
    "@typescript-eslint/parser": "^4.16.1",
    "babel-eslint": "^10.1.0",
    "enzyme": "^3.11.0",
    "enzyme-adapter-react-16": "^1.15.6",
    "eslint-config-airbnb": "^18.2.1",
    "eslint-config-airbnb-base": "^14.2.1",
    "eslint-config-prettier": "^8.1.0",
    "eslint-import-resolver-typescript": "^2.4.0",
    "eslint-plugin-import": "^2.22.1",
    "eslint-plugin-jsx-a11y": "6.2.3",
    "eslint-plugin-prettier": "^3.3.1",
    "eslint-plugin-react": "7.21.5",
    "eslint-plugin-react-hooks": "2.5.0",
    "gh-pages": "^3.1.0",
    "prettier": "^2.2.1",
    "react-test-renderer": "^17.0.1"
  

Jest.config.ts

 module.exports = 
  rootDir: 'src/tests',
  testRegex: '/src/tests/.*test\\.tsx$',
  setupFiles: ['<rootDir>/setupTests.ts'],
;

setupTests.js

/* eslint-disable import/no-extraneous-dependencies */
import Adapter from 'enzyme-adapter-react-16';
import  configure  from 'enzyme';

configure( adapter: new Adapter() );

【问题讨论】:

【参考方案1】:

您必须更改测试脚本以接受配置。赞这个react-scripts test -- --config jest.config.js

注意额外的-- 参数。

【讨论】:

以上是关于CRA 忽略 jest.config 文件的主要内容,如果未能解决你的问题,请参考以下文章

如果 jest.config.js 在子文件夹或子目录中,则 Jest 需要更多时间

如何将文件添加到忽略列表中

android svn怎么忽略文件

Sourcetree忽略文件

git 配置忽略文件规则

SVN添加忽略后,怎么解除被忽略的文件