Karma/Jasmine测试在从10.1更新到11.0/12.0后破碎



从10.1->12.0,但是当从10.1->11.0

ng test编译并打开web浏览器,但测试无法运行。每个测试都失败,并出现以下错误: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

我在其他几个来源中发现,zone.js/dist/zone-testing需要是test.ts文件中的第一个导入。作为升级的一部分,import语句被更改为import 'zone.js/testing';。我试过两者的每一种组合,但没有一种能让这些测试奏效。

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"
}
}

因果报应.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"
}
}

有人能帮忙吗?

尝试生成一个全新的Angular 12.0.3项目并运行单元测试。如果这样做有效,那么很可能存在配置问题。

转到项目的package.json,将单元测试的关键包与新生成的包进行比较(查找karmakarma-jasminejasmine(,并确保它们匹配。一旦它们匹配,删除package-lock.json并运行新的npm安装。

最后,对两个项目的test.ts进行了比较。希望问题能解决。

发现问题是由我的tsconfig设置引起的。不确定我是否遗漏了文档,但我不得不将模块设置更改为es2020,将目标设置更改为es2017

感谢AliF50建议使用新项目来比较配置。经过大约8个小时的比较和测试,我找到了答案。

最新更新