Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 660
El Dude Avatar asked Feb 06 '19 00:02

El Dude


1 Answers

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

like image 103
Deepak Jha Avatar answered Oct 14 '22 14:10

Deepak Jha