简体   繁体   English

当我运行 npm 运行测试时,Jest 遇到了意外的令牌

[英]Jest encountered an unexpected token when I run the npm run test

Good day.再会。 I create a app using react and add the library of @testing-library/react.我使用 react 创建了一个应用程序并添加了 @testing-library/react 库。 I also install use-global-hook and I was having an error when I run the npm run test.我还安装了 use-global-hook 并且在运行 npm 运行测试时出现错误。 anyone could help me to get out to this error.任何人都可以帮助我摆脱这个错误。 Thanks谢谢

here's my pakage.json.这是我的包装。json。 [package.json][1] [包.json][1]

{
  "name": "trial",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@babel/core": "^7.9.6",
    "@testing-library/jest-dom": "^4.2.4",
    "@testing-library/user-event": "^7.1.2",
    "react": "^16.13.1",
    "react-dom": "^16.13.1",
    "react-router-dom": "^5.1.2",
    "react-scripts": "3.4.1",
    "use-global-hook": "^0.1.12"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "ie 11",
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {
    "@babel/plugin-transform-modules-commonjs": "^7.9.6",
    "@testing-library/react": "^9.5.0",
    "react-test-renderer": "^16.13.1"
  }
}

here's the error details这是错误详细信息

    Jest encountered an unexpected token

    This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.

    By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".

    Details:

    E:\Backups\Projects\React\trial\node_modules\use-global-hook\index.js:60
    export default useStore;
    ^^^^^^

    SyntaxError: Unexpected token 'export'

      1 | import React from 'react';
    > 2 | import globalHook from 'use-global-hook';
        | ^
      3 | import actions from './actions';
      4 | import initState from './initState';
      5 | 

      at ScriptTransformer._transformAndBuildScript (node_modules/@jest/transform/build/ScriptTransformer.js:537:17)
      at ScriptTransformer.transform (node_modules/@jest/transform/build/ScriptTransformer.js:579:25)
      at Object.<anonymous> (src/services/useGLobal.js:2:1)

I checked the use-global-hook repo.我检查了use-global-hook repo。 It seems that this package is distributed in ES Module.似乎这个 package 分布在 ES Module 中。 You will need to set up transformer for Jest so it understands ES module syntax.您需要为 Jest 设置转换器,以便它理解 ES 模块语法。

This is the documentation for Jest transform config 这是 Jest 转换配置的文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM