尽管规范文件存在,Karma 在 angular 项目中显示 "Incomplete: no spec found"

Karma shows "Incomplete: no spec found" in angular project although spec files exist

我已经在几个组件的规范文件中编写了测试用例,但是当我 运行 ng-test 它仍然显示 "Incomplete: no specs found, , randomized with seed ######"。我的业力文件看起来像这样

// 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')
    ],
    client: {
      clearContext: false // leave Jasmine Spec Runner output visible in browser
    },
    coverageIstanbulReporter: {
      dir: require('path').join(__dirname, './coverage/tenms'),
      reports: ['html', 'lcovonly', 'text-summary'],
      fixWebpackSourcePaths: true
    },
    reporters: ['progress', 'kjhtml'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['Chrome'],
    singleRun: false,
    restartOnFileChange: true
  });
};

src 文件夹中的条目 test.ts 文件如下所示

// This file is required by karma.conf.js and loads recursively all the .spec and framework files

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 Runner 停止的任何编译器错误,或者您可以按如下方式查找 test.ts 文件-

// 然后我们找到所有的测试。 const context = require.context('./', true, /.spec.ts$/);

确保在此处允许所有 spec.ts。

得到答案。我必须更正 spec.ts 文件中的所有错误以避免编译时错误。之后一切正常运行。