Cannot find module @ionic/angular (ionic 4) from test-file

Explosion Pills

I'm trying to use jest to test an Ionic app. My jest configuration is:

preset: 'jest-preset-angular',
setupTestFrameworkScriptFile: '<rootDir>/src/setupJest.ts',
transformIgnorePatterns: ['node_modules/(?!@ngrx|@ionic-native|@ionic)'],
globals: {
  __TRANSFORM_HTML__: true,
},

The setupJest.js file has import 'jest-preset-angular';.

I have a very simple test where I use TestBed to create an instance of the App component and check that it's truthy. In this code I have import { Platform } from '@ionic/angular';.

Jest errors with:

Cannot find module '@ionic/angular' from 'app.component.test.ts'

If I just remove the import { Platform } from '@ionic/angular' from the test file, Jest gives me the same error from the component file:

Cannot find module '@ionic/angular' from 'app.component.ts'

Using ionic serve / building works, jest is just giving me this error that it can't import X from Y.

How can I get jest to import from @ionic files?

Explosion Pills

I was able to resolve this from the information in the linked GitHub issue. I arrived at the final configurations linked here: https://github.com/ionic-team/ionic/issues/15695#issuecomment-428412641

Since, I have modified these configurations to speed up the tests (with cache) and still have not run into issues. My current configurations:

// jest.config.js
module.exports = {
  globals: {
    // jest-preset-angular sets ts-jest tsConfigFile as "src/tsconfig.spec.json". This overwrites
    // it to use the default `tsconfig.json`. You can change this to your preferred config.
    __TRANSFORM_HTML__: true,
  },
  preset: 'jest-preset-angular',
  setupTestFrameworkScriptFile: '<rootDir>/src/setupJest.ts',
  moduleNameMapper: {
    '^@ionic/core/loader': '<rootDir>/node_modules/@ionic/core/dist/esm/es5/ionic.define.js',
  },
  transformIgnorePatterns: [
    '<rootDir>/node_modules/(?!@ionic)',
  ],
};

src/setupJest.ts only includes import 'jest-preset-angular'.

TypeScript configuration:

{
  "compilerOptions": {
    "allowJs": true,
    "target": "esnext",
    "module": "commonjs",
    "moduleResolution": "node",
    "declaration": false,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "forceConsistentCasingInFileNames": true,
    "noEmit": true,
    "skipLibCheck": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "lib": ["es6", "dom"]
  }
}

You can probably vary some of these configuration settings quite a bit, but the most important one is allowJs. This is required or else jest won't be able to import a lot of the Ionic libraries properly.

Finally, remember to import from /ngx with Ionic 4 files. For example:

import { StatusBar } from '@ionic-native/status-bar/ngx';

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

After changing file extension from js to ts, ionic cannot find module any more

From Dev

Error: "Cannot find module" when cloning Ionic 4 component with StencilJS

From Dev

Ionic 3 Angular 4 Cannot find module `jsonwebtoken`

From Dev

Error: Cannot find module 'symbol-observable' in Ionic 4

From Dev

Jest is unable to find module from test file

From Dev

cannot find module file

From Dev

Cannot find module at AVA test

From Dev

error TS2307: Cannot find module '@ionic-native/date-picker/ngx' in ionic 4

From Dev

Cannot find module 'ionic-angular'.ts when version upgraded to Ionic 4

From Dev

I got err `Cannot find module 'react' from 'makeStyles.js' ` when I import a component in the test file

From Dev

Dynamic Environment Variables in Ionic - Cannot find module

From Dev

Cannot find module generated with “Ionic g provider”

From Dev

Ionic 2 - Runtime error Cannot find module "."

From Dev

Ionic 2 cannot find module 'dgram'

From Dev

Cannot find module "ionic-native"

From Dev

Ionic => Runtime error: Cannot find module "."

From Dev

Ionic/Angular Issue Cannot find module './fs'

From Dev

Cannot find Ionic-angular module

From Dev

Cannot find module from local file in express js app

From Dev

Cannot find module, pointed to file

From Dev

Ionic 4 / Angular Routing: ERROR Error: Uncaught (in promise): Error: Cannot find 'EventHomePageModule' in './home/home.module'

From Dev

Cannot find module 'components/layouts/default/Banner' from 'test/banner.test.js'

From Dev

TS2307: Cannot find module 'ionic/ionic'

From Dev

Runtime Error Cannot find module "ionic-native" IONIC 2

From Dev

"Cannot find module 'three' from"

From Dev

Error when run the test "Cannot find module "

From Dev

Cannot find module with **/*.test.js

From Dev

Cannot find module "d3" error is displayed after i upgraded my project from ionic 2 to ionic 3

From Dev

Jest test: Cannot find module '../Link.react' from 'Link.react.js'

Related Related

  1. 1

    After changing file extension from js to ts, ionic cannot find module any more

  2. 2

    Error: "Cannot find module" when cloning Ionic 4 component with StencilJS

  3. 3

    Ionic 3 Angular 4 Cannot find module `jsonwebtoken`

  4. 4

    Error: Cannot find module 'symbol-observable' in Ionic 4

  5. 5

    Jest is unable to find module from test file

  6. 6

    cannot find module file

  7. 7

    Cannot find module at AVA test

  8. 8

    error TS2307: Cannot find module '@ionic-native/date-picker/ngx' in ionic 4

  9. 9

    Cannot find module 'ionic-angular'.ts when version upgraded to Ionic 4

  10. 10

    I got err `Cannot find module 'react' from 'makeStyles.js' ` when I import a component in the test file

  11. 11

    Dynamic Environment Variables in Ionic - Cannot find module

  12. 12

    Cannot find module generated with “Ionic g provider”

  13. 13

    Ionic 2 - Runtime error Cannot find module "."

  14. 14

    Ionic 2 cannot find module 'dgram'

  15. 15

    Cannot find module "ionic-native"

  16. 16

    Ionic => Runtime error: Cannot find module "."

  17. 17

    Ionic/Angular Issue Cannot find module './fs'

  18. 18

    Cannot find Ionic-angular module

  19. 19

    Cannot find module from local file in express js app

  20. 20

    Cannot find module, pointed to file

  21. 21

    Ionic 4 / Angular Routing: ERROR Error: Uncaught (in promise): Error: Cannot find 'EventHomePageModule' in './home/home.module'

  22. 22

    Cannot find module 'components/layouts/default/Banner' from 'test/banner.test.js'

  23. 23

    TS2307: Cannot find module 'ionic/ionic'

  24. 24

    Runtime Error Cannot find module "ionic-native" IONIC 2

  25. 25

    "Cannot find module 'three' from"

  26. 26

    Error when run the test "Cannot find module "

  27. 27

    Cannot find module with **/*.test.js

  28. 28

    Cannot find module "d3" error is displayed after i upgraded my project from ionic 2 to ionic 3

  29. 29

    Jest test: Cannot find module '../Link.react' from 'Link.react.js'

HotTag

Archive