gpt4 book ai didi

reactjs - CRA 忽略 jest.config 文件

转载 作者:行者123 更新时间:2023-12-05 06:03:56 24 4
gpt4 key购买 nike

我正在使用带有 typescript 和 enzyme 的 React 16,但 jest 没有读取我的适配器所在的 setupTests.ts 文件。如果我尝试在所有测试开始时配置适配器,它会工作,但是当我尝试这样做时,它会给我 Enzyme expects an adapter to be configured 错误。使用 typescript 时是否需要一些特定的配置或其他东西?

包.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() });

最佳答案

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

注意额外的 -- 参数。

关于reactjs - CRA 忽略 jest.config 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66504780/

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