El Dude
El Dude

Reputation: 5618

Jest Ionic test fails - unexpected token import

I am not sure why importing GA into my unit test fails. How can I fix this?

Maybe the JEST error should point me into the right direction, but unfortunately it does not:

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';

By instruction I extended my package.json with

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

but it still fails with the same error

Upvotes: 7

Views: 1019

Answers (1)

Deepak Jha
Deepak Jha

Reputation: 1609

I think I have found a solution to this problem,

  1. Do not run npm run eject in your code base,
  2. You can pass the flag in your package.json test command as given below, --env=jsdom --transformIgnorePatterns 'node_modules/(?!(@ionic|<other-package-need-to-transform>)/)

this solution is common for ionic/react and ionic/angular

Upvotes: 3

Related Questions