After upgrading from Angular 2 to Angular 4, there were some delightful new errors in my unit tests
After some initial hair-pulling, I’ve finally got my unit tests working again after upgrading from Angular 2 to Angular 4. But once I started running the tests, I found these lovely errors:
1 2 3 4 5 6 7 8 9 10 11 |
ERROR in [at-loader] ./path-to/example-service.spec.ts TS2304: Cannot find name 'describe'. ERROR in [at-loader] ./path-to/example-service.spec.ts TS2304: Cannot find name 'beforeEach'. ERROR in [at-loader] ./path-to/example-service.spec.ts TS2304: Cannot find name 'it'. ERROR in [at-loader] ./path-to/example-service.spec.ts TS2304: Cannot find name 'expect'. |
I knew it could not be the unit tests themselves; it had to be something low-level. Then it occurred to me that in my Angular 2 implementation, I had a typings.json file in the root of my application to handle types such as jasmine , lodash and moment. But with Angular 4, types are handled in package.json via the @types namespace.
So, after some searching, I found this Stackoverflow article: Angular 2 Unit Tests: Cannot find name ‘describe’. The solution provided worked perfectly for me.
Two Steps:
1 – In package.json, add this line to your devDependencies:
1 |
"@types/jasmine": "^2.6.0", |
2 – And then in your unit test file (i.e. XXX.spec.ts), add this line:
1 |
import {} from 'jasmine'; |
All of the errors that complain about describe, beforeEach, expect and it should no longer appear in your terminal.
Major credit goes to ksharifbd for this answer. This was a major time-saver!