Doe*_*Joe 5 reactjs jestjs babeljs create-react-app unexpected-token
当尝试运行测试时,Jest 会抛出遇到的意外令牌错误。我正在使用 ky http,这显然会导致问题并且不会被转译。
\n\n Test suite failed to run\n\n Jest encountered an unexpected token\n\n This usually means that you are trying to import a file which Jest cannot parse, e.g. it\'s not plain JavaScript.\n\n By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".\n\n Here\'s what you can do:\n \xe2\x80\xa2 To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.\n \xe2\x80\xa2 If you need a custom transformation specify a "transform" option in your config.\n \xe2\x80\xa2 If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.\n\n You\'ll find more details and examples of these config options in the docs:\n https://jestjs.io/docs/en/configuration.html\n\n Details:\n\n Z:\\sepview.web\\sepview-react\\node_modules\\ky\\index.js:277\n export default createInstance();\n ^^^^^^\n\n SyntaxError: Unexpected token export\n\n 1 | import * as React from \'react\';\n > 2 | import ky from \'ky\';\n | ^\n 3 |\n 4 | class HttpService extends React.Component {\n 5 |\n\n at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:403:17)\n at Object.<anonymous> (src/components/HttpService.tsx:2:1)\n\nTest Suites: 1 failed, 1 total\nRun Code Online (Sandbox Code Playgroud)\n\n这是我的 package.json
\n\n{\n "name": "test",\n "version": "0.1.0",\n "private": true,\n "dependencies": {\n "@fortawesome/fontawesome-svg-core": "^1.2.7",\n "@fortawesome/free-solid-svg-icons": "^5.4.2",\n "@fortawesome/react-fontawesome": "^0.1.3",\n "@types/d3": "^5.0.1",\n "bootstrap": "^4.1.3",\n "bootswatch": "^4.1.3",\n "d3": "^5.7.0",\n "d3-array": "^2.0.2",\n "jquery": "^3.3.1",\n "ky": "^0.5.1",\n "moment": "^2.22.2",\n "node-sass": "^4.9.4",\n "popper.js": "^1.14.4",\n "react": "^16.6.0",\n "react-dom": "^16.6.0",\n "react-router": "^4.3.1",\n "react-router-dom": "^4.3.1",\n "react-scripts": "2.1.1",\n "typescript": "^3.1.6"\n },\n "scripts": {\n "start": "react-scripts start",\n "build": "react-scripts build",\n "test": "react-scripts test",\n "eject": "react-scripts eject"\n },\n "eslintConfig": {\n "extends": "react-app"\n },\n "browserslist": [\n ">0.2%",\n "not dead",\n "not ie <= 11",\n "not op_mini all"\n ],\n "devDependencies": {\n "@types/enzyme": "^3.1.14",\n "@types/enzyme-adapter-react-16": "^1.0.3",\n "@types/jest": "^23.3.9",\n "@types/node": "^10.12.2",\n "@types/react": "^16.4.18",\n "@types/react-dom": "^16.0.9",\n "@types/react-router": "^4.4.0",\n "@types/react-router-dom": "^4.3.1",\n "enzyme": "^3.7.0",\n "enzyme-adapter-react-16": "^1.6.0"\n },\n "proxy": "http://localhost:43947"\n}\nRun Code Online (Sandbox Code Playgroud)\n\n将transform或transformIgnorePatterns添加到package.json时,如下所示:
\n\n"jest": {\n "transform": {\n "^.+\\\\.js$": "babel-jest"\n },\n "transformIgnorePatterns": [\n "/node_modules/(?!(@ky)/).*/"\n ]\n },\nRun Code Online (Sandbox Code Playgroud)\n\n我收到此错误:
\n\nOut of the box, Create React App only supports overriding these Jest options:\n\n \xe2\x80\xa2 collectCoverageFrom\n \xe2\x80\xa2 coverageReporters\n \xe2\x80\xa2 coverageThreshold\n \xe2\x80\xa2 globalSetup\n \xe2\x80\xa2 globalTeardown\n \xe2\x80\xa2 resetMocks\n \xe2\x80\xa2 resetModules\n \xe2\x80\xa2 snapshotSerializers\n \xe2\x80\xa2 watchPathIgnorePatterns.\n\nThese options in your package.json Jest configuration are not currently supported by Create React App:\n\n \xe2\x80\xa2 transform\n \xe2\x80\xa2 transformIgnorePatterns\n\nIf you wish to override other Jest options, you need to eject from the default setup. You can do so by running npm run eject but remember that this is a one-way operation. You may also file an issue with Create React App to discuss supporting more options out of the box.\nRun Code Online (Sandbox Code Playgroud)\n\n我还有一个 babel.config.js ,它是空的 atm:
\n\nmodule.exports = function () {\n const presets = [];\n const plugins = [];\n\n return {\n presets,\n plugins\n };\n }\nRun Code Online (Sandbox Code Playgroud)\n\n弹出不是一个选择。我尝试解决这个问题超过一周。非常感谢任何帮助。
\n我遇到了类似的问题,material-ui-next-pickers在 dist/ 中使用 es6 导入
这是因为material-ui-next-pickers在 Jest 中运行之前,\xe2\x80\x99 并未被转译。create-react-app排除一切./node_modules。
我们可以通过更改来解决此问题,但问题中指出的transformIgnorePatterns不支持它。create-react-app
但是,我们仍然可以将其作为命令参数传递给react-script test --transformIgnorePatterns \\"node_modules/(?!my-module)/\\"
例子
\n\n "scripts": {\n "test": "react-scripts test --transformIgnorePatterns \\"node_modules/(?!material-ui-next-pickers)/\\" --env=jsdom",\n ...\n\n }\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
2378 次 |
| 最近记录: |