reactjs - 语法错误: Unexpected token m in JSON at position 0: Jest fails running tests all of a sudden

标签 reactjs jestjs enzyme

我的所有测试突然失败了。

该错误相当模糊且低级。我不确定我到底应该做什么以及这意味着什么......

我尝试过: - 在全局范围内安装了jest - 全局卸载 jest 并在本地运行 (jest 24.8.0) - 更改了我的节点版本较低和较高(当前使用 10,尝试过 8,并且之前可以使用 10)

下面是我收到的错误...

  ● Test suite failed to run

    SyntaxError: Unexpected token m in JSON at position 0
        at JSON.parse (<anonymous>)

      3 |  *
      4 |  * @link http://airbnb.io/enzyme/docs/installation/#working-with-react-16
    > 5 |  * @copyright 2017 Airbnb, Inc.
        |               ^
      6 |  */
      7 | // const enzyme = require("enzyme");
      8 | // const Adapter = require("enzyme-adapter-react-16");

      at Runtime._loadModule (node_modules/jest-runtime/build/index.js:568:59)
      at Object.<anonymous> (node_modules/entities/lib/decode_codepoint.js:1:106)
      at Object.<anonymous> (node_modules/htmlparser2/lib/Tokenizer.js:3:23)
      at Object.<anonymous> (node_modules/htmlparser2/lib/Parser.js:1:106)
      at Object.<anonymous> (node_modules/htmlparser2/lib/index.js:1:103)
      at Object.<anonymous> (node_modules/cheerio/lib/parse.js:4:18)
      at Object.<anonymous> (node_modules/cheerio/lib/cheerio.js:5:13)
      at Object.<anonymous> (node_modules/cheerio/index.js:5:28)
      at Object.<anonymous> (node_modules/enzyme/build/ReactWrapper.js:15:16)
      at Object.<anonymous> (node_modules/enzyme/build/index.js:3:21)
      at Object.<anonymous> (src/test-setup.js:5:15)

最佳答案

我遇到了同样的问题,花了 4 个多小时调试这个问题,尝试添加 babel、配置插件等等......我决定向同事寻求帮助,3000 万后他确实意识到唯一的事情需要的是:

更新:

'^.+\\.js': 'babel-jest',

'^.+\\.js$': 'babel-jest',

注意末尾的 $...babel-jest 在 json 上运行,就像在 js 上一样...

关于reactjs - 语法错误: Unexpected token m in JSON at position 0: Jest fails running tests all of a sudden,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56103772/

相关文章:

reactjs - 测试 redux Action 时,调度不是函数

css - 如何在 React 应用程序的不同组件上加载不同的 .css 文件?

javascript - 如何在 react-router Route 中从 onChange 调用 dispatch?

javascript - Reactjs:为什么子组件不在数据更改时重新呈现?

javascript - 使用 Jest 在 vanilla js 中测试 AJAX

typescript - 开 Jest : Test recursive call inside Promise

reactjs - 如何使用 jest 和 react-testing-library 测试上下文提供的函数?

node.js - 关于管理 React node_modules 的建议

javascript - 期望 this.children.props()

javascript - 什么时候应该使用 Jest 快照测试与单元测试中的常规断言?