Reputation: 81
Went from 10.1 -> 12.0, but the same problem exists when going from 10.1 -> 11.0
ng test
compiles and opens web browser, but tests are unable to run. Every test fails with the following error: Error: zone-testing.js is needed for the fakeAsync() test helper but could not be found. Please make sure that your environment includes zone.js/testing
.
I've found in several other sources that zone.js/dist/zone-testing
needs to be the first import within test.ts
file. As part of the upgrade, the import statement was changed to import 'zone.js/testing';
. I've tried every combination of the two, and none of them gets these tests working.
package.json
{
"name": "smart-price-v2",
"version": "0.0.1",
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"buildDeploy": "ng build --prod --configuration production --verbose=true",
"test": "ng test",
"test:headless": "ng test --browsers=ChromeHeadless --codeCoverage=true --watch=false",
"lint": "ng lint",
"e2e": "ng e2e"
},
"private": true,
"dependencies": {
"@angular/animations": "~12.0.3",
"@angular/cdk": "~12.0.3",
"@angular/common": "~12.0.3",
"@angular/compiler": "~12.0.3",
"@angular/core": "~12.0.3",
"@angular/flex-layout": "~12.0.0-beta.34",
"@angular/forms": "~12.0.3",
"@angular/material": "~12.0.3",
"@angular/material-moment-adapter": "~12.0.3",
"@angular/platform-browser": "~12.0.3",
"@angular/platform-browser-dynamic": "~12.0.3",
"@angular/router": "~12.0.3",
"@angular/service-worker": "~12.0.3",
"@azure/msal-angular": "^2.0.0",
"@azure/msal-browser": "^2.14.2",
"angular-google-charts": "^2.2.0",
"angular-moment": "^1.3.0",
"moment": "^2.29.1",
"ngx-feature-toggle": "^10.0.0",
"rxjs": "~6.6.0",
"tslib": "^2.1.0",
"zone.js": "~0.11.4"
},
"devDependencies": {
"@angular-devkit/build-angular": "~12.0.3",
"@angular/cli": "~12.0.3",
"@angular/compiler-cli": "~12.0.3",
"@angular/language-service": "~12.0.3",
"@types/jasmine": "~3.6.0",
"@types/node": "^12.11.1",
"codelyzer": "^6.0.1",
"jasmine-core": "~3.7.0",
"jasmine-spec-reporter": "~6.0.0",
"karma": "~6.3.0",
"karma-chrome-launcher": "~3.1.0",
"karma-coverage": "~2.0.3",
"karma-coverage-istanbul-reporter": "~3.0.3",
"karma-jasmine": "~4.0.0",
"karma-jasmine-html-reporter": "^1.5.0",
"karma-junit-reporter": "^2.0.1",
"protractor": "~7.0.0",
"ts-node": "~9.0.0",
"tslint": "~6.1.0",
"tslint-eslint-rules": "^5.4.0",
"typescript": "~4.2.3"
}
}
karma.conf.js
// Karma configuration file, see link for more information
// https://karma-runner.github.io/1.0/config/configuration-file.html
module.exports = function (config) {
config.set({
basePath: '',
frameworks: ['jasmine', '@angular-devkit/build-angular'],
plugins: [
require('karma-jasmine'),
require('karma-chrome-launcher'),
require('karma-jasmine-html-reporter'),
require('karma-coverage'),
require('@angular-devkit/build-angular/plugins/karma'),
require('karma-junit-reporter')
],
client: {
clearContext: false // leave Jasmine Spec Runner output visible in browser
},
jasmineHtmlReporter: {
suppressAll: true // removes the duplicated traces
},
coverageReporter: {
dir: require('path').join(__dirname, './coverage/SmartPriceV2'),
subdir: '.',
reporters: [
{ type: 'html' },
{ type: 'text-summary' }
]
},
reporters: ['progress', 'kjhtml', 'junit'],
junitReporter: {
outputDir: './junit/SmartPriceV2'
},
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true, // false,
browsers: ['Chrome'],
singleRun: false,
restartOnFileChange: true
});
};
angular.json
{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"version": 1,
"newProjectRoot": "projects",
"projects": {
"SmartPriceV2": {
"projectType": "application",
"schematics": {
"@schematics/angular:component": {
"style": "scss"
}
},
"root": "",
"sourceRoot": "src",
"prefix": "app",
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
"outputPath": "dist/SmartPrice",
"index": "src/index.html",
"main": "src/main.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "tsconfig.app.json",
"assets": [
"src/favicon.ico",
"src/assets",
"src/manifest.webmanifest",
"src/web.config",
"src/akamai"
],
"styles": [
"./node_modules/@angular/material/prebuilt-themes/indigo-pink.css",
"src/styles.scss"
],
"scripts": [],
"vendorChunk": true,
"extractLicenses": false,
"buildOptimizer": false,
"sourceMap": true,
"optimization": false,
"namedChunks": true
},
"configurations": {
"production": {
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.deploy.ts"
}
],
"optimization": true,
"outputHashing": "all",
"sourceMap": false,
"extractCss": true,
"namedChunks": false,
"extractLicenses": true,
"vendorChunk": false,
"buildOptimizer": true,
"budgets": [
{
"type": "initial",
"maximumWarning": "2mb",
"maximumError": "5mb"
},
{
"type": "anyComponentStyle",
"maximumWarning": "6kb",
"maximumError": "10kb"
}
],
"serviceWorker": true,
"ngswConfigPath": "ngsw-config.json"
},
"test": {
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.test.ts"
}
],
"optimization": true,
"outputHashing": "all",
"sourceMap": false,
"extractCss": true,
"namedChunks": false,
"extractLicenses": true,
"vendorChunk": false,
"buildOptimizer": true,
"budgets": [
{
"type": "initial",
"maximumWarning": "3mb",
"maximumError": "10mb"
},
{
"type": "anyComponentStyle",
"maximumWarning": "6kb",
"maximumError": "10kb"
}
],
"serviceWorker": true,
"ngswConfigPath": "ngsw-config.json"
}
}
},
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
"options": {
"browserTarget": "SmartPriceV2:build"
},
"configurations": {
"production": {
"browserTarget": "SmartPriceV2:build:production"
},
"test": {
"browserTarget": "SmartPriceV2:build:test"
}
}
},
"extract-i18n": {
"builder": "@angular-devkit/build-angular:extract-i18n",
"options": {
"browserTarget": "SmartPriceV2:build"
}
},
"test": {
"builder": "@angular-devkit/build-angular:karma",
"options": {
"main": "src/test.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "tsconfig.spec.json",
"karmaConfig": "karma.conf.js",
"inlineStyleLanguage": "scss",
"assets": [
"src/favicon.ico",
"src/assets"
],
"styles": [
"src/styles.scss"
],
"scripts": []
}
},
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"options": {
"tsConfig": [
"tsconfig.app.json",
"tsconfig.spec.json",
"e2e/tsconfig.json"
],
"exclude": [
"**/node_modules/**"
]
}
},
"e2e": {
"builder": "@angular-devkit/build-angular:protractor",
"options": {
"protractorConfig": "e2e/protractor.conf.js",
"devServerTarget": "SmartPriceV2:serve"
},
"configurations": {
"production": {
"devServerTarget": "SmartPriceV2:serve:production"
},
"test": {
"devServerTarget": "SmartPriceV2:serve:test"
}
}
}
}
}
},
"defaultProject": "SmartPriceV2",
"cli": {
"analytics": "422ba9c2-8e7b-4eb7-8c0f-5e588d5241bd"
}
}
Can anyone help?
Upvotes: 4
Views: 2306
Reputation: 81
Turns out the issue was caused by my tsconfig settings. Not sure if I missed documentation somewhere, but I had to change the module setting to es2020
and the target setting to es2017
.
Thanks to AliF50 for suggesting to use a new project to compare configurations. After about 8 hours of comparing and testing, I found the answer.
Upvotes: 3
Reputation: 18809
Try generating a brand new Angular 12.0.3
project and run the unit tests. If that works, then most likely there is a configuration issue.
Go to the package.json
of your project and compare critical packages to unit testing to the newly generated one (look for karma
, karma-jasmine
, jasmine
) and ensure they match. Once they do match, delete package-lock.json and run a fresh npm install.
Finally, compare test.ts
between the two projects. Hopefully, that resolves.
Upvotes: 3