【问题标题】:Jest Ionic test fails - unexpected token importJest Ionic 测试失败 - 意外的令牌导入
【发布时间】:2019-06-29 21:24:34
【问题描述】:

我不确定为什么将 GA 导入我的单元测试会失败。我该如何解决这个问题?

也许 JEST 错误应该为我指明正确的方向,但不幸的是它没有:

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".

....

/Users/me/app/node_modules/@ionic-native/google-analytics/index.js:20
    import { Injectable } from '@angular/core';
    ^^^^^^

    SyntaxError: Unexpected token import

      2 | import { Http } from '@angular/http';
      3 | import { Events } from 'ionic-angular';
    > 4 | import { GoogleAnalytics } from '@ionic-native/google-analytics';

根据指示,我扩展了我的package.json

"transformIgnorePatterns": [
  "<rootDir>/node_modules"
],

但它仍然失败并出现同样的错误

【问题讨论】:

标签: angularjs unit-testing ionic-framework ionic3 jestjs


【解决方案1】:

我想我已经找到了解决这个问题的方法,

  1. 不要在您的代码库中运行npm run eject
  2. 您可以在package.json 测试命令中传递标志,如下所示, --env=jsdom --transformIgnorePatterns 'node_modules/(?!(@ionic|&lt;other-package-need-to-transform&gt;)/)

这种解决方案对于 ionic/react 和 ionic/angular 很常见

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-24
    • 2018-01-21
    • 2019-02-06
    • 2018-04-25
    • 2019-03-27
    • 2018-01-20
    • 2018-01-17
    • 2018-12-10
    相关资源
    最近更新 更多