Recently, I upgraded my Angular project from version 15 to 15.1 and encountered an error while running tests. To replicate the issue, I created a new Angular 15.1 project using the CLI and generated a service with similar semantics to the one causing problems in my project.
Service
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class StorageServiceService {
public static readonly A = 'a';
public static readonly B = StorageServiceService.A + 'b';
public static readonly C = StorageServiceService.A + 'c';
constructor() { }
}
Test
import { TestBed } from '@angular/core/testing';
import { StorageServiceService } from './storage-service.service';
describe('StorageServiceService', () => {
let service: StorageServiceService;
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(StorageServiceService);
});
it('should be created', () => {
expect(service).toBeTruthy();
});
it('test', () => {
expect(StorageServiceService.A).toEqual('a')
expect(StorageServiceService.B).toEqual('ab')
expect(StorageServiceService.C).toEqual('ac')
})
});
Running the tests resulted in the following error:
Chrome 109.0.0.0 (Mac OS 10.15.7) ERROR
An error was thrown in afterAll
Uncaught TypeError: Cannot read properties of undefined (reading 'A')
TypeError: Cannot read properties of undefined (reading 'A')
at Object.2494 (http://localhost:9877/_karma_webpack_/webpack:/src/app/storage-service.service.ts:8:52)
at __webpack_require__ (http://localhost:9877/_karma_webpack_/webpack:/webpack/bootstrap:19:1)
at Object.283 (http://localhost:9877/_karma_webpack_/main.js:86:82)
at __webpack_require__ (http://localhost:9877/_karma_webpack_/webpack:/webpack/bootstrap:19:1)
at __webpack_exec__ (http://localhost:9877/_karma_webpack_/main.js:186:48)
at http://localhost:9877/_karma_webpack_/main.js:187:126
at Function.__webpack_require__.O (http://localhost:9877/_karma_webpack_/webpack:/webpack/runtime/chunk loaded:23:1)
at http://localhost:9877/_karma_webpack_/main.js:188:56
at webpackJsonpCallback (http://localhost:9877/_karma_webpack_/webpack:/webpack/runtime/jsonp chunk loading:34:1)
I am currently using Node 18.12.1 with npm 9.2.0.
package.json
{
"name": "test-app",
"version": "0.0.0",
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"watch": "ng build --watch --configuration development",
"test": "ng test"
},
"private": true,
"dependencies": {
"@angular/animations": "^15.1.0",
"@angular/common": "^15.1.0",
"@angular/compiler": "^15.1.0",
"@angular/core": "^15.1.0",
"@angular/forms": "^15.1.0",
"@angular/platform-browser": "^15.1.0",
"@angular/platform-browser-dynamic": "^15.1.0",
"@angular/router": "^15.1.0",
"rxjs": "~7.8.0",
"tslib": "^2.3.0",
"zone.js": "~0.12.0"
},
"devDependencies": {
"@angular-devkit/build-angular": "^15.1.1",
"@angular/cli": "~15.1.1",
"@angular/compiler-cli": "^15.1.0",
"@types/jasmine": "~4.3.0",
"jasmine-core": "~4.5.0",
"karma": "~6.4.0",
"karma-chrome-launcher": "~3.1.0",
"karma-coverage": "~2.2.0",
"karma-jasmine": "~5.1.0",
"karma-jasmine-html-reporter": "~2.0.0",
"typescript": "~4.9.4"
}
}
Reverting back to Angular 15 resolved the issue. The upgrade process was done using the angular CLI automatically.