angular - Karma 测试运行两次
问题描述
我们有一个在 jasmine 中进行单元测试的 Angular 项目。我遇到一个问题,每次保存文件时,我的所有单元测试都会运行两次(使用自动监视)。
我发现这篇文章显示了类似的问题:https ://pracucci.com/avoid-karma-tests-run-twice-on-autowatch.html 。由于我们的文件在 Typescript 中,我尝试了这个:
{ pattern: "**/*.ts", watched: false, served: true, included: true }
,但如果我将文件模式设置为 Typescript,这将不起作用。它给出了以下错误:WARN [middleware:karma]: Invalid file type (ts),默认为 js。
这是我的 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-istanbul-reporter'),
require('@angular-devkit/build-angular/plugins/karma'),
require('karma-junit-reporter')
],
client : {
clearContext: false // leave Jasmine Spec Runner output visible in browser
},
coverageIstanbulReporter: {
dir : require('path').join(__dirname, '../coverage'),
reports : ['html', 'cobertura', 'lcovonly'],
fixWebpackSourcePaths: true
},
jasmine: {
random: false
},
junitReporter: {
outputdir: '',
outputFile: 'test-angular.xml'
},
reporters : ['progress', 'kjhtml', 'junit'],
port : 9876,
colors : true,
logLevel : config.LOG_INFO,
autoWatch : true,
autoWatchBatchDelay : 10000,
browsers : ['Chrome'],
singleRun : false,
files: [
{ pattern: '../node_modules/@angular/material/prebuilt-themes/indigo-pink.css', included: true, watched: true }
]
});
};
这是我的 angular.json 文件:
{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"version": 1,
"newProjectRoot": "projects",
"projects": {
"fuse": {
"root": "",
"sourceRoot": "src",
"projectType": "application",
"prefix": "app",
"schematics": {
"@schematics/angular:component": {
"styleext": "scss"
}
},
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
"outputPath": "dist",
"index": "src/index.html",
"main": "src/main.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "src/tsconfig.app.json",
"assets": [
"src/favicon.ico",
"src/assets"
],
"styles": [
"src/styles.scss",
"node_modules/ngx-toastr/toastr.css"
],
"scripts": [
"node_modules/highcharts/highstock.js",
"node_modules/highcharts/modules/exporting.js"
],
"showCircularDependencies": false
},
"configurations": {
"production": {
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
}
],
"optimization": true,
"outputHashing": "all",
"sourceMap": false,
"extractCss": true,
"namedChunks": false,
"aot": true,
"extractLicenses": true,
"vendorChunk": false,
"buildOptimizer": true,
"budgets": [
{
"type": "initial",
"maximumWarning": "2mb",
"maximumError": "5mb"
}
]
},
"ec": {
"sourceMap": true,
"extractCss": true
},
"hmr": {
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.hmr.ts"
}
]
}
}
},
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
"options": {
"browserTarget": "fuse:build"
},
"configurations": {
"production": {
"browserTarget": "fuse:build:production"
},
"hmr": {
"hmr": true,
"browserTarget": "fuse:build:hmr"
},
"ec": {
"browserTarget": "fuse:build:ec"
}
}
},
"extract-i18n": {
"builder": "@angular-devkit/build-angular:extract-i18n",
"options": {
"browserTarget": "fuse:build"
}
},
"test": {
"builder": "@angular-devkit/build-angular:karma",
"options": {
"codeCoverage": false,
"codeCoverageExclude": ["src/@fuse/**/*"],
"main": "src/test.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "src/tsconfig.spec.json",
"karmaConfig": "src/karma.conf.js",
"styles": [
"src/styles.scss"
],
"scripts": [],
"assets": [
"src/favicon.ico",
"src/assets"
]
}
},
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"options": {
"tsConfig": [
"src/tsconfig.app.json",
"src/tsconfig.spec.json"
],
"exclude": [
"**/node_modules/**"
]
}
}
}
},
"fuse-e2e": {
"root": "e2e/",
"projectType": "application",
"prefix": "",
"architect": {
"e2e": {
"builder": "@angular-devkit/build-angular:protractor",
"options": {
"protractorConfig": "e2e/protractor.conf.js",
"devServerTarget": "fuse:serve"
},
"configurations": {
"production": {
"devServerTarget": "fuse:serve:production"
}
}
},
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"options": {
"tsConfig": "e2e/tsconfig.e2e.json",
"exclude": [
"**/node_modules/**"
]
}
}
}
}
},
"defaultProject": "fuse"
}
这是我的 tests.ts 文件:
// This file is required by karma.conf.js and loads recursively all the .spec and framework files
import 'core-js/es6/map';
import 'core-js/es7/reflect';
import 'zone.js/dist/zone-testing';
import { getTestBed } from '@angular/core/testing';
import {
BrowserDynamicTestingModule,
platformBrowserDynamicTesting
} from '@angular/platform-browser-dynamic/testing';
declare const require: any;
// First, initialize the Angular testing environment.
getTestBed().initTestEnvironment(
BrowserDynamicTestingModule,
platformBrowserDynamicTesting()
);
// Then we find all the tests.
const context = require.context('./', true, /\.spec\.ts$/);
// And load the modules
context.keys().map(context);
如何修复 Karma 以仅在更改时运行一次单元测试?
解决方案
更新到最新版本的 Angular 修复了它
推荐阅读
- django - 仅包含(继承)特定的模板块
- javascript - 如何在html中选择特定的时间范围
- math - 使用递归树求解递归方程
- android - 运行由 autoninja 生成的 Android 演示项目出现错误:Entry name 'org/appspot/apprtc/R.class' collided
- python - 为更大的数据创建指标矩阵?
- html - 如何在 ReactJS 中的图像上添加文本?
- android - Frida 服务器没有足够的权限
- c - while 语句中的 print 语句被打印出超过所需的次数
- python - 如何跟踪“熊猫”数据框中的无序对
- php - Wordpress nav walker - 将 SVG 箭头图标添加到父菜单项