我已将 Angular 从 15 升级到 16,但是当我运行 ng 测试时,它不包含任何 component.spec.ts 文件,并且所有测试用例都失败了:
package.json:
"karma": "6.3.17",
"karma-chrome-launcher": "3.1.0",
"karma-cli": "1.0.1",
"karma-firefox-launcher": "^1.0.1",
"karma-jasmine": "^5.1.0",
"karma-jasmine-html-reporter": "^2.1.0",
"karma-junit-reporter": "^2.0.1",
"karma-spec-reporter": "0.0.32",
"karma-typescript": "^5.4.0",
"karma-coverage": "^2.2.0",
"jasmine-auto-spies": "^8.0.1",6
tsconfig.json:
{
"compileOnSave": false,
"compilerOptions": {
"downlevelIteration": true,
"importHelpers": true,
"outDir": "./dist/out-tsc",
"sourceMap": true,
"declaration": false,
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"noUnusedLocals": false,
"target": "es2021",
"typeRoots": ["node_modules/@types", "facebook-js-sdk"],
"lib": ["es2019", "dom"],
"module": "esnext",
"baseUrl": "./src",
"useDefineForClassFields": false,
"paths": {
"@components/*": [
"app/shared/components/*",
"app/core/components/*",
"app/route-controller/pages/virtual-visits/components/*",
"app/route-controller/pages/virtual-visits/components/schedules-visits/components/*"
],
"@pipes/*": ["app/shared/pipes/*"],
"@services/*": ["app/core/services/*"],
"@directives/*": ["app/shared/directives/*"],
"@core/*": ["app/core/*"],
"@builders/*": ["app/domain/builder/*"],
"@entities/*": ["app/domain/entities/*"],
"@enums/*": ["app/domain/enums/*"],
"@interfaces/*": ["app/domain/interfaces/*"],
"@domain/*": ["app/domain/*"],
"@pages/*": ["app/pages/*", "app/route-controller/pages/*"],
"@app/*": ["app/*"]
}
},
"exclude": ["src/app/route-controller/pages/**/mocks/*.json"],
"angularCompilerOptions": {
"fullTemplateTypeCheck": true,
"strictTemplates": true,
"enableIvy": true
}
}
karma.conf.js:
module.exports = function (config) {
config.set({
basePath: '',
frameworks: ['jasmine', '@angular-devkit/build-angular'],
plugins: [
require('karma-jasmine'),
require('karma-typescript'),
require('karma-spec-reporter'),
require('karma-junit-reporter'),
require('karma-chrome-launcher'),
require('karma-firefox-launcher'),
require('karma-jasmine-html-reporter'),
require('karma-coverage'),
require('@angular-devkit/build-angular/plugins/karma'),
],
specReporter: {
maxLogLines: 5,
suppressErrorSummary: false,
suppressFailed: false,
suppressPassed: false,
suppressSkipped: false,
showSpecTiming: false,
failFast: false,
},
client: {
clearContext: false, // leave Jasmine Spec Runner output visible in browser
},
coverageReporter: {
dir: require('path').join(__dirname, 'coverage'),
reporters: [
{ type: 'html' },
{ type: 'text-summary' },
{ type: 'lcovonly' },
],
fixWebpackSourcePaths: true,
check: {
global: {
statements: 50,
branches: 15,
functions: 45,
lines: 50,
},
},
},
captureTimeout: 60000,
browserDisconnectTimeout: 60000,
browserDisconnectTolerance: 3,
browserNoActivityTimeout: 60000,
reporters: ['spec', 'progress', 'kjhtml', 'junit'],
junitReporter: {
outputDir: '../../../../test-reports/angular/', // results will be saved as $outputDir/$browserName.xml
outputFile: undefined, // if included, results will be saved as $outputDir/$browserName/$outputFile
suite: '', // suite will become the package name attribute in xml testsuite element
useBrowserName: true, // add browser name to report and classes names
nameFormatter: undefined, // function (browser, result) to customize the name attribute in xml testcase element
classNameFormatter: undefined, // function (browser, result) to customize the classname attribute in xml testcase element
properties: {}, // key value pair of properties to add to the <properties> section of the report
xmlVersion: null, // use '1' if reporting to be per SonarQube 6.2 XML format
},
port: 9876,
colors: true,
logLevel: config.ERROR,
browsers: ['CustomChromeHeadless'],
customLaunchers: {
CustomChromeHeadless: {
base: 'ChromeHeadless',
flags: ['--no-sandbox'],
},
},
});
};
我不确定我在这里错过了什么。我在执行
ng test
: 时遇到以下错误
您似乎有
describe
块,但没有 it
测试。
类似这样的:
describe('abc', () => {
// empty here, no it('...
});
它好像不喜欢这样。
如果您没有上述内容,我要做的是生成一个空白的 Angular 16 项目并比较
angular.json
、karma.conf.js
、package.json
、tsconfig.json
、karma.conf.js
和 tsconfig.spec.json
文件希望找出可能导致问题的原因。