Jest Ionic测试失败-意外的令牌导入

时间:2019-02-06 00:07:46

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

我不确定为什么将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"
],

但是它仍然失败,并显示相同的错误

1 个答案:

答案 0 :(得分:0)

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

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

此解决方案通常用于离子/反应和离子/角