ionic-framework: Jest unit testing no longer works with Ionic 4 beta 9-11
Bug Report
Ionic Info
Run ionic info from a terminal/cmd prompt and paste the output below.
Ionic:
ionic (Ionic CLI) : 4.1.2
Ionic Framework : @ionic/angular 4.0.0-beta.11
@angular-devkit/core : 0.8.3
@angular-devkit/schematics : 0.8.3
@angular/cli : 6.2.3
@ionic/ng-toolkit : 1.0.7
@ionic/schematics-angular : 1.0.6
Cordova:
cordova (Cordova CLI) : 7.1.0
Cordova Platforms : none
Cordova Plugins : no whitelisted plugins (0 plugins total)
System:
Android SDK Tools : 26.1.1 (/Users/rc101077/Library/Android/sdk)
ios-deploy : 2.0.0
ios-sim : 7.0.0
NodeJS : v8.11.4 (/Users/rc101077/.nvm/versions/node/v8.11.4/bin/node)
npm : 6.4.1
OS : macOS High Sierra
Xcode : Xcode 9.4.1 Build version 9F2000
Describe the Bug Jest unit testing no longer works with Ionic 4 beta 9. An error appears for each spec file that relies on Ionic as such:
Cannot find module '@ionic/core/loader' from 'app-initialize.js'
at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:221:17)
at Object.<anonymous> (node_modules/@ionic/angular/dist/app-initialize.js:1:1)
Steps to Reproduce Steps to reproduce the behavior:
- Create an Ionic project with a Jest unit test setup (Jest version 22+)
- Run the unit tests
- See the error above for every file using Ionic imports
Expected Behavior There should be no errors and all the unit tests should pass.
Additional Context This looks to be broken with Ionic 4 beta 9, 10 and 11. It works fine in beta 8.
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 6
- Comments: 37 (13 by maintainers)
Commits related to this issue
- Updating Ionic from default configuration to work with jest tests. See: https://github.com/ionic-team/ionic/issues/15695\#issuecomment-457308086 for more information — committed to ajcrites/ionic-jest-testing-example by ajcrites 5 years ago
- Fix jest tests Sources: * https://github.com/ionic-team/ionic/issues/15695#issuecomment-490511950 * https://stackoverflow.com/questions/51830523/jest-ionic-4-beta-import-platform-from-ionic-angular-g... — committed to Bielik20/nx-mean-starter by Bielik20 5 years ago
FYI, this problem still exists in Ionic beta 12. But in addition, the same tests in beta 12 now show this error:
I’ve tried adding
"^@ionic/angular": "<rootDir>/node_modules/@ionic/angular/dist/index",to themoduleNameMapperof Jest, but that just changes the error to:For now, I’m reverting back to beta 11 until a solution can be found.
I experience the same issue. Since
jest-preset-angularreceived a major update to version 7 russcarver/ionic4-project-template stopped working for me.I spent a little time investigating the problem and set up a new ionic project using the following package-versions
Here is the sample repository: GregOnNet/ionic-4-jest-setup
I wrote a small article discussing why @ionic/angular fails to run with Jest: How to set up Jest in an Ionic 4 project
I am glad if this helps somebody.
I put in a temporary fix for this by substituting the missing check into a replacement script loaded by Jest.
Now I have:
and my
svgoverride.jsscript is:Feel free to check it out on my repo (currently master or v0.0.2). I’ve also updated a lot of packages to the latest: https://github.com/russcarver/ionic4-project-template/tree/master
Ultimately my problem was not having
allowJsin my TypeScript configuration. My configurations:Jest configuration:
tsconfig:
Hi,
Same issue here ! That’s because
@ionic/core/loaderpackage.json has nomainproperty. Looking forward to find how to tell jest to resolvemoduleproperty.EDIT
It works with
But should be better to not add this…
Found the issue. Needs an allowJs added to the tsconfig.spec.json file.
I’ve taken another look at this now that Ionic v4 has been published. This issue can probably be closed, although arguably there are some changes that can be made to
tsconfig.spec.json.My latest updates + walkthrough for jest setup:
Sample repository: https://github.com/ajcrites/ionic-jest-testing-example
First,
npm install --save-dev jest jest-preset-angular.Create your jest configuration as you see fit. Minimally you will need:
You will also need to create the
setupTestFrameworkScriptFile. You can make this file whatever you want. I’ve named itsrc/setupJest.tsand put it there. It should at least contain:If you are using
tsconfig.spec.jsonyou will have to add"module": "commonjs"and"allowJs": true". Mine looked like this after updating:Now you’re all set to test with jest.
e2e should still work, but it does depend on karma and jasmine.
The default test for
app.component.spec.tsusesjasminefor some things, so you will have to update it if you want to use it. I’ve done this in the sample repo: https://github.com/ajcrites/ionic-jest-testing-example/blob/master/src/app/app.component.spec.ts.Individual needs for TypeScript configuration will also vary. I got the tests to work using just:
@russcarver @paulstelzer I’ve tested with beta 17. Most of the advice in this thread still applies, summarized here:
allowJsneeds to betruefor TypeScript config.svgoverride.jsneeds to be mapped if you have tests that import Ionic modules. This is true regardless of whether your app has or uses any svgs at all.From what I can see, the
@ionic/core/loadermapping is no longer needed.My currently working jest configuration:
The
svgoverride.jsis what you provided:Note that
allowJsis not set in the configuration created byionic start. You will have to add it to whatever TypeScript configuration you use for testing.In summary, the only fix for jest I would suggest is to update/fix
svgoverride.jsto check forrequire.context, although I’m not sure why this is needed. It’s possibly something that can be fixed on the jest side.I’m not sure why
allowJsis not in the default config either, but adding it may have other implications. I haven’t seen this yet, though. It could possibly be enabled fortsconfig.spec.json, but I’m not sure if that would impact the jasmine tests.I’m also not 100% sure that the core module mapper is not needed. It might be needed in some other cases.
Yes! Here’s a link to my Ionic4 starter repo that I’ve been keeping up to date over the months.
https://github.com/russcarver/ionic4-project-template
The master branch is up to date as of today and should have what you need.
Don’t forget you need
/ngxon the end of that import.It works with my fix never tried without it. And I no longer work on an Ionic project atm sorry Liam
Hi @liamdebeasi,
sorry, but I cannot answer your question. I haven’t worked with Ionic for a couple of months and have no time to investigate in this issue.
Still having the issue. As everyone said previously I had to add this to my jest config (jest-setup.ts or package.json > “jest” object) :
and this to my tsconfig.spec.json :
See https://github.com/facebook/jest/issues/2663
There are potentially many ways to fix it. I updated my jest config like so and it seems to be working:
Seems like the svgoverride.js may no longer be needed, though.
@ajcrites love you man! 😛 I’m hours trying to figure out that!