gpt4 book ai didi

angular - 将 Angular 从 10.1 更新到 11.0/12.0 后,Karma/Jasmine 测试损坏

转载 作者:行者123 更新时间:2023-12-05 02:42:00 25 4
gpt4 key购买 nike

从 10.1 -> 12.0,但从 10.1 -> 11.0 时存在同样的问题

ng test 编译并打开网络浏览器,但测试无法运行。每个测试都失败并出现以下错误:错误:fakeAsync() 测试助手需要 zone-testing.js 但找不到。请确保您的环境包含 zone.js/testing

我在其他几个来源中发现 zone.js/dist/zone-testing 需要是 test.ts 文件中的第一个导入。作为升级的一部分,导入语句更改为 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"
}
}

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

有人能帮忙吗?

最佳答案

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

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

最后,比较两个项目的test.ts。希望这能解决。

关于angular - 将 Angular 从 10.1 更新到 11.0/12.0 后,Karma/Jasmine 测试损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67876798/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com