业力测试运行两次

时间:2019-12-12 10:06:10

标签: angular typescript unit-testing jasmine 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]:无效的文件类型(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在更改时仅运行一次单元测试?

1 个答案:

答案 0 :(得分:1)

更新到最新的Angular版本已修复

相关问题