Let's start by addressing the current state of my project on the /develop
branch, which is all in order with passing tests.
To improve code readability, I decided to create a branch specifically for cleaning up the imports
and implementing aliases instead of repeatedly using ../../../../
to access classes. I made the necessary changes in the tsconfig.json
file as follows:
"baseUrl": "src",
"paths": {
"@app/*": [
"app/*"
],
"@core/*": [
"app/core/*"
],
"@common/*": [
"app/common/*"
],
"@models/*": [
"app/models/*"
],
"@env/*": [
"environments/*"
],
"@assets/*": [
"assets/*"
]
}
After completing this task, running the tests using npm run test
, which essentially executes something like
karma start ./karma.conf.js --log-level error
, resulted in the following error:
HeadlessChrome 67.0.3396 (Windows 10.0.0) ERROR
Uncaught Error: Missing: SyncTestZoneSpec
at http://localhost:9876/_karma_webpack_/vendor.bundle.js:270128
The error message seems related to the changes made in the imports and aliases. Any insights on what this error implies?
Update: Issue resolved with GitHub links
After updating the zone.js version to 0.8.26 and simplifying the imports in test.ts to just one line:
import 'zone.js/dist/zone-testing';
However, a new error emerged across all tests:
HeadlessChrome 67.0.3396 (Windows 10.0.0) SomeService #getCurrentUser should return user object FAILED
TypeError: Cannot read property 'assertPresent' of undefined
at resetFakeAsyncZone node_modules/@angular/core/@angular/core/testing.es5.js:308:1)
at Object.<anonymous> node_modules/@angular/core/@angular/core/testing.es5.js:1015:1)
at ZoneQueueRunner.webpackJsonp../node_modules/zone.js/dist/zone-testing.js.jasmine.QueueRunner.ZoneQueueRunner.execute node_modules/zone.js/dist/zone-testing.js:437:1)
HeadlessChrome 67.0.3396 (Windows 10.0.0): Executed 120 of 120 (120 FAILED) ERROR (4.725 secs / 4.633 secs)
Reference to a related issue on GitHub can be found here, though no solution is currently available.
This is the content of my test.ts
:
// This file is required by karma.conf.js and loads recursively all the .spec and framework files
import { getTestBed } from '@angular/core/testing';
import { BrowserDynamicTestingModule, platformBrowserDynamicTesting } from '@angular/platform-browser-dynamic/testing';
import 'zone.js/dist/zone-testing';
// Unfortunately there's no typing for the `__karma__` variable. Just declare it as any.
declare const __karma__: any;
declare const require: any;
// Prevent Karma from running prematurely.
__karma__.loaded = function () {};
// Initialize the Angular testing environment.
getTestBed().initTestEnvironment(
BrowserDynamicTestingModule,
platformBrowserDynamicTesting()
);
// Find and load all tests.
const context = require.context('./', true, /\.spec\.ts$/);
context.keys().map(context);
// Start Karma to run the tests.
__karma__.start();