nx: getComponentFromError when running Jest in webstorm
Ticket to provide the solution for people that meet the issue when running jest tests by using @nrwl/builders:jest + webstorm.
Tests are running well by using ng test but don’t run when using webstorm + jest:
TypeError: Cannot read property 'getComponentFromError' of null
at TestBedViewEngine._initIfNeeded (/home/user/dev/packages/core/testing/src/test_bed.ts:393:46)
at TestBedViewEngine.get (/home/user/dev/packages/core/testing/src/test_bed.ts:473:10)
at Function.TestBedViewEngine.get (/home/user/dev/packages/core/testing/src/test_bed.ts:243:36)
at Object.get (/home/user/dev/company/project/node_modules/@netbasal/spectator/bundles/ng:/@netbasal/spectator/lib/service.ts:56:22)
at Object.<anonymous> (/home/user/dev/company/project/apps/sot-desktop/src/app/features/station-note/station-plan.state.service.spec.ts:16:39)
at Object.asyncJestLifecycle (/home/user/dev/company/project/node_modules/jest-jasmine2/build/jasmineAsyncInstall.js:53:37)
at resolve (/home/user/dev/company/project/node_modules/jest-jasmine2/build/queueRunner.js:43:12)
at new Promise (<anonymous>)
at mapper (/home/user/dev/company/project/node_modules/jest-jasmine2/build/queueRunner.js:26:19)
at promise.then (/home/user/dev/company/project/node_modules/jest-jasmine2/build/queueRunner.js:73:41)
at process.internalTickCallback (internal/process/next_tick.js:77:7)
This issue is related to #862 and #747
Libraries:
- @angular: 7.1.15
- @nrwl: 7.8.6
- @netbasal/spectator: 3.9.1
- jest: 24.8.0
- jest-preset-angular: 7.1.1
- ts-jest: 24.0.2
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 1
- Comments: 16 (2 by maintainers)
Commits related to this issue
- test(frontend): set `setupFilesAfterEnv` - to run in Jest tests within WebStorm - https://github.com/nrwl/nx/issues/1439#issuecomment-561268656 — committed to PhilippeMorier/talus by PhilippeMorier 5 years ago
- Release 0.0.1 (#7) * build(dep): `@babylonjs` & `@types/node`: - @babylonjs:4.1.0-beta.9 - @types/node:12.12.16 * feat(apps/frontend): select initial tool * fix(apps/frontend): import hamme... — committed to PhilippeMorier/talus by PhilippeMorier 5 years ago
- Release v0.0.2 (#15) * build(dep): `@babylonjs` & `@types/node`: - @babylonjs:4.1.0-beta.9 - @types/node:12.12.16 * feat(apps/frontend): select initial tool * fix(apps/frontend): import ham... — committed to PhilippeMorier/talus by PhilippeMorier 4 years ago
- Release v0.0.3 (#23) * build(dep): `@babylonjs` & `@types/node`: - @babylonjs:4.1.0-beta.9 - @types/node:12.12.16 * feat(apps/frontend): select initial tool * fix(apps/frontend): import ham... — committed to PhilippeMorier/talus by PhilippeMorier 4 years ago
- Release v0.0.4 (#35) * build(dep): `@babylonjs` & `@types/node`: - @babylonjs:4.1.0-beta.9 - @types/node:12.12.16 * feat(apps/frontend): select initial tool * fix(apps/frontend): import ham... — committed to PhilippeMorier/talus by PhilippeMorier 4 years ago
- build(CircleCi): set upstream (#36) * build(dep): `@babylonjs` & `@types/node`: - @babylonjs:4.1.0-beta.9 - @types/node:12.12.16 * feat(apps/frontend): select initial tool * fix(apps/fronte... — committed to PhilippeMorier/talus by PhilippeMorier 4 years ago
- Release v0.0.5 (#39) * build(dep): `@babylonjs` & `@types/node`: - @babylonjs:4.1.0-beta.9 - @types/node:12.12.16 * feat(apps/frontend): select initial tool * fix(apps/frontend): import ham... — committed to PhilippeMorier/talus by PhilippeMorier 4 years ago
- refactor: resolve conflicts (#48) * build(CircleCi): set upstream (#36) * build(dep): `@babylonjs` & `@types/node`: - @babylonjs:4.1.0-beta.9 - @types/node:12.12.16 * feat(apps/frontend): s... — committed to PhilippeMorier/talus by PhilippeMorier 4 years ago
- Release 0.0.6 (#49) * build(dep): `@babylonjs` & `@types/node`: - @babylonjs:4.1.0-beta.9 - @types/node:12.12.16 * feat(apps/frontend): select initial tool * fix(apps/frontend): import hamm... — committed to PhilippeMorier/talus by PhilippeMorier 4 years ago
The only config that I had to add in my
apps/app-name/jest.config.jsissetupFilesAfterEnv:@vsavkin - This problem still exists with Nx library schematics. It appears that developers must still manually apply this fix to the jest config:
@ronnyek, ok so I found a solution, at least for us internally, it might work for you as well maybe (?).
We have a project with several libs. Each lib has its own jest.config.js which refer to the “main” jest.config.js.
I had to:
import 'jest-preset-angular';Check if this works.
Though I think it broke the command line execution because it seems Nrwl is using a deprecated command “setupTestFrameworkScriptFile” in their jest files instead of “setupFilesAfterEnv”.
So we had to know when the test was launched from Webstorm or command line. For that I simply set an environment variable in the Webstorm Jest template:
Then in the jest.config.js I am testing if the environment variable is set or not:
I guess you might have to adapt for your uses…
Options: setupTestFrameworkScriptFile and setupFilesAfterEnv cannot be used together.😞My solution:
add
setupFilesAfterEnv: ["<rootDir>/src/test-setup.ts"]tojest.conf.jsand remove"setupFile": "<...>/src/test-setup.ts"inangular.json.It works for both
npm testand run with webstorm.For some guys got the error:
connect ECONNREFUSED 127.0.0.1:80 even all test passedwhen run component tests, you probably need to add this tojest.conf.js:Found that specifying
setupFilefor builder overrides all and anysetupFilesAfterEnv.https://github.com/nrwl/nx/blob/9c50569c55090d5ec7d16d97ac9a829de5aea9b1/packages/jest/src/builders/jest/jest.impl.ts#L118-L122
Tests that involve nothing with scss files, actually do run with what I had setup before… but there is something about this that is failing, but only when launched from webstorm.
It’s currently complaining about not being able to load “icon.scss” which doesn’t error.
I dont know if its related or not, but I noticed I get this output even when the other tests run and complete successfully.
And all this is after trying to make some of the changes you suggested