到处探索但没有找到可靠的方法来解决它,所以决定发布一个问题。
在 Angular 15 中运行时,一切正常;但一旦升级到 Angular 17,它就检测不到任何单元测试,并显示“执行了 0 个成功,共 0 个成功”。
我创建了一个新的、独立的 Angular 17 项目来比较配置,但似乎没有什么明显的不同。单元测试本身可以在新项目中检测到,所以它们应该没问题。
在这里发布一些配置代码。如果需要任何进一步的信息,也请告诉我。
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: [
'karma-jasmine',
'karma-chrome-launcher',
'karma-jasmine-html-reporter',
'karma-coverage',
'karma-junit-reporter',
'@angular-devkit/build-angular/plugins/karma'
],
client: {
clearContext: false // leave Jasmine Spec Runner output visible in browser
},
coverageReporter: {
dir: require('path').join(__dirname, './coverage'),
type: 'lcov',
subdir: '.',
reports: ['html', 'lcovonly', 'text-summary'],
fixWebpackSourcePaths: true,
check: {
global: {
statements: 80,
branches: 80,
functions: 80,
lines: 80
}
}
},
reporters: ['progress', 'junit', 'kjhtml','coverage'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
customLaunchers: {
ChromeHeadlessNoSandbox: {
base: 'ChromiumHeadless',
flags: ['--no-sandbox']
}
},
junitReporter : {
outputDir : './junit/test-reports'
},
browsers: ['Chrome'],
singleRun: false,
restartOnFileChange: true
});
};
angular.json 中的“测试”部分:
"test": {
"builder": "@angular-devkit/build-angular:karma",
"options": {
"polyfills": ["zone.js", "zone.js/testing"],
"tsConfig": "tsconfig.spec.json",
"karmaConfig": "karma.conf.js",
"codeCoverage": true,
"assets": [
"src/favicon.ico",
"src/assets",
"src/manifest.webmanifest"
],
"styles": [
"src/styles.scss",
"src/styles/spartacus/storefinder.scss",
"src/styles/spartacus/user.scss",
"src/styles/spartacus/qualtrics-embedded-feedback.scss",
"src/styles/spartacus/product.scss",
"src/styles/spartacus/pdf-invoices.scss"
],
"scripts": []
}
},
升级后,Angular 有时会表现得很奇怪,您是否尝试过关闭编辑器(甚至可能重新启动),然后再次运行 ng test