-
Notifications
You must be signed in to change notification settings - Fork 13.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Jest unit testing no longer works with Ionic 4 beta 9-11 #15695
Comments
My package.json Jest config "jest": {
"preset": "jest-preset-angular",
"setupTestFrameworkScriptFile": "<rootDir>/src/setupJest.ts",
"moduleNameMapper": {
"^@app/(.*)": "<rootDir>/src/app/$1",
"^@ionic/core/loader": "<rootDir>/node_modules/@ionic/core/loader/loader.ts"
},
"transformIgnorePatterns": [
"<rootDir>/node_modules/(?!@ngrx|@ionic-native|@ionic)"
]
} |
Hi, Same issue here ! That's because
EDIT It works with
But should be better to not add this... |
Thanks @paulsouche your |
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
For now, I'm reverting back to beta 11 until a solution can be found. |
Using version 11 of the beta with the suggested configuration above, I get:
from I can fix this using babel: https://stackoverflow.com/questions/52651907/unexpected-token-export-with-jest ...but then I get a very strange unexpected token error from the import loader .js files. |
Don't forget you need |
@russcarver thank you, but now I get |
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. |
Ultimately my problem was not having Jest configuration:
tsconfig:
|
Glad you figured it out! |
Hi, Since beta.12 (and in beta.13), @russcarver Error
In beta.11 require.context was tested
Looking forward for a workaround trying to declare require.context in jest setup but didn't find one yet... If anyone has a clue help is really appreciated. Thanks |
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
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: |
@ajcrites love you man! :P I'm hours trying to figure out that! |
Thanks for your issue! I think you made workarounds, correct? Could you please explain how it can be fixed or maybe push a PR? Otherwise I would set it under investigation |
You can see my temporary fix in my last comment above. I have not tried removing the fix since to see if anything has changed. |
@russcarver @paulstelzer I've tested with beta 17. Most of the advice in this thread still applies, summarized here:
From what I can see, the My currently working jest configuration: module.exports = {
preset: 'jest-preset-angular',
// just has `import 'jest-preset-angular';` as required
setupTestFrameworkScriptFile: '<rootDir>/src/setupJest.ts',
moduleNameMapper: {
'^@ionic/core/dist/ionic/svg': '<rootDir>/svgoverride.js',
},
transformIgnorePatterns: [
'<rootDir>/node_modules/(?!@ngrx|@ionic)',
],
}; The if (require && require.context) {
require.context('!!file-loader?name=[name].[ext]&outputPath=svg!./', false, /.svg$/);
} Note that In summary, the only fix for jest I would suggest is to update/fix I'm not sure why I'm also not 100% sure that the core module mapper is not needed. It might be needed in some other cases. |
New error on beta.19
|
See jestjs/jest#2663 There are potentially many ways to fix it. I updated my jest config like so and it seems to be working: {
transform: {
'^.+\\.svg$': 'jest-transform-stub',
},
preset: 'jest-preset-angular',
// imports 'jest-preset-angular'
setupTestFrameworkScriptFile: '<rootDir>/src/setupJest.ts',
// added 'ionicons'
transformIgnorePatterns: [
'<rootDir>/node_modules/(?!@ngrx|@ionic|ionicons)',
],
}; Seems like the svgoverride.js may no longer be needed, though. |
Yeah i tried that but get this:
|
@ajcrites, thanks. That worked for me. |
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 My latest updates + walkthrough for jest setup: Sample repository: https://github.com/ajcrites/ionic-jest-testing-example First, Create your jest configuration as you see fit. Minimally you will need: {
preset: 'jest-preset-angular',
setupTestFrameworkScriptFile: '<rootDir>/src/setupJest.ts',
transformIgnorePatterns: [
'<rootDir>/node_modules/(?!@ionic)',
],
}; You will also need to create the import 'jest-preset-angular'; If you are using {
"extends": "../tsconfig.json",
"compilerOptions": {
"outDir": "../out-tsc/spec",
"types": [
"jasmine",
"node"
],
"module": "commonjs",
"allowJs": true
},
"files": [
"test.ts",
"polyfills.ts"
],
"include": [
"**/*.spec.ts",
"**/*.d.ts"
]
} 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 Individual needs for TypeScript configuration will also vary. I got the tests to work using just: {
"compilerOptions": {
"module": "commonjs",
"allowJs": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true
}
} |
I was able to get the hacks removed finally. I've removed the following section completely.
I'm now using:
And my ionic/cordova config is:
For Angular, I'm using 7.2.7 I will note that Jest 24.0.0+ makes some changes that are breaking. I have not had time to figure out all of these yet. |
I have updated my Git repo with the changes (and updates to Ionic4 and Angular7). https://github.com/russcarver/ionic4-project-template |
My current setup is: // jest config
module.exports = {
preset: 'jest-preset-angular',
setupFilesAfterEnv: ['<rootDir>/src/setupJest.ts'],
transformIgnorePatterns: [
'<rootDir>/node_modules/(?!@ngrx|@ionic|ionicons)',
],
}; // setupJest.ts
import 'jest-preset-angular'; tsconfig: {
"compilerOptions": {
"target": "esnext",
"module": "commonjs",
"moduleResolution": "node",
"declaration": false,
"noUnusedLocals": true,
"noUnusedParameters": true,
"forceConsistentCasingInFileNames": true,
"lib": ["dom", "es7"],
"skipLibCheck": true,
"noEmit": true,
"allowJs": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true
}
} This works without any hacks per se with:
However, there may be certain other libraries or items tested that may require additional hacks. |
I experience the same issue. 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. |
@GregOnNet Looks like something changed in the deps for https://github.com/GregOnNet/ionic-4-jest-setup in the last couple weeks that broke the method this repo uses to work.
Gets this error:
I am going to hack on it a bit and see if I find what changed, but wanted to check to see if you have seen this breakage as well. |
Found the issue. Needs an allowJs added to the tsconfig.spec.json file. |
Thank you for the issue and all of the debugging by everyone! Would anyone be willing to submit a PR to our Angular starter for what all needs to change to make this work? Here is the source for it: https://github.com/ionic-team/starters/tree/master/angular/base |
I have updated https://github.com/russcarver/ionic4-project-template/tree/master with the upgrade as well. |
Thank so much i spend my whole week for this error |
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 :
|
@Yohandah Worked for me! |
Hi everyone, Does this issue still reproduce in the latest version of Ionic 6? |
Hi @liamdebeasi, sorry, but I cannot answer your question. |
It works with my fix never tried without it. And I no longer work on an Ionic project atm sorry Liam |
No problem at all. I am going to close this issue for now, but feel free to reply to this if anyone is still running into issues. Thanks! |
Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Ionic, please create a new issue and ensure the template is fully filled out. |
Bug Report
Ionic Info
Run
ionic info
from a terminal/cmd prompt and paste the output below.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:
Steps to Reproduce
Steps to reproduce the behavior:
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.
The text was updated successfully, but these errors were encountered: