首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Jest with Create React App:测试套件无法运行-意外令牌

Jest with Create React App:测试套件无法运行-意外令牌
EN

Stack Overflow用户
提问于 2018-03-22 19:16:15
回答 2查看 3.3K关注 0票数 8

开箱即用jest的create-react-app总是失败

我所做的步骤

代码语言:javascript
复制
create-react-app cra-test
cd cra-test
yarn install

package.json中的原始test更改为

代码语言:javascript
复制
{
  "name": "cra-test",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "react": "^16.2.0",
    "react-dom": "^16.2.0",
    "react-scripts": "1.1.1"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "jest",
    "eject": "react-scripts eject"
  }
}

当我运行yarn test时,测试失败。下面是输出

代码语言:javascript
复制
yarn run v1.1.0
$ jest
FAIL  src\App.test.js
  ● Test suite failed to run

    .../cra-test/src/App.test.js: Unexpected token (7:18)
         5 | it('renders without crashing', () => {
         6 |   const div = document.createElement('div');
      >  7 |   ReactDOM.render(<App />, div);
           |                   ^
         8 |   ReactDOM.unmountComponentAtNode(div);
         9 | });
        10 |

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        3.413s, estimated 12s
Ran all test suites.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

我读到这可能是因为升级了Jest,导致它与create-react-app不兼容,但我没有这样做。

代码语言:javascript
复制
node --version && yarn --version && npm --version && create-react-app --version
v6.10.2
1.1.0
5.7.1
1.4.3
EN

回答 2

Stack Overflow用户

发布于 2018-05-25 20:23:21

除非你弹出create-react-app,否则你的根目录中既没有babel config也没有jest config,因此jest不知道它必须转译源代码。所以你必须创建.babelrc:

代码语言:javascript
复制
{
 "presets": [
   "react-app"
  ]
}

和jest.config.js:

代码语言:javascript
复制
module.exports = {
  "collectCoverageFrom": [
    "src/**/*.{js,jsx,mjs}"
  ],
  "setupFiles": [
    "<rootDir>/config/polyfills.js"
  ],
  "testMatch": [
    "<rootDir>/src/**/__tests__/**/*.{js,jsx,mjs}",
    "<rootDir>/src/**/?(*.)(spec|test).{js,jsx,mjs}"
  ],
  "testEnvironment": "node",
  "testURL": "http://localhost",
  "transform": {
    "^.+\\.(js|jsx|mjs)$": "<rootDir>/node_modules/babel-jest",
    "^.+\\.css$": "<rootDir>/config/jest/cssTransform.js",
    "^(?!.*\\.(js|jsx|mjs|css|json)$)": "<rootDir>/config/jest/fileTransform.js"
  },
  "transformIgnorePatterns": [
    "[/\\\\]node_modules[/\\\\].+\\.(js|jsx|mjs)$"
  ],
  "moduleNameMapper": {
    "^react-native$": "react-native-web"
  },
  "moduleFileExtensions": [
    "web.js",
    "mjs",
    "js",
    "json",
    "web.jsx",
    "jsx",
    "node"
  ]
}

这些配置可以在执行npm run eject之后找到。

因为jest config引用了babel-preset-react-app,所以应该安装它。还要注意,jest config提到了config目录,它是create-react-app的一部分。这意味着没有它就不能工作,所以你需要从create-react-app复制这个目录,或者编写你自己的设置文件。

票数 1
EN

Stack Overflow用户

发布于 2018-06-14 12:38:50

我不知道原因,但我在下面的时刻修复了它。export CI=1 Bibliography

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49427264

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档