将Angular从5升级到7,并尝试使用ng test命令运行test。之后,我得到“找不到规格”

时间:2019-04-02 07:37:29

标签: jasmine angular-cli karma-jasmine angular7 angular-cli-v7

尝试通过命令ng test运行测试并得到错误“未找到规格”和Chrome 73.0.3683(Linux 0.0.0)后,将角度从5更新为7:执行0 of 0错误(0.029秒/ 0秒)

有人可以帮助我解决此问题吗?我已经尝试了所有可能的方案来解决此问题。实际上,当我运行ng test时,它应该找到我的spec.ts文件来运行脚本,但没有发生


我的cli版本:

Angular CLI:7.3.9

Node: 8.16.0

OS: linux x64

Angular: 7.2.15


Package Version
@angular-devkit/architect 0.12.4
@angular-devkit/build-angular 0.12.4
@angular-devkit/build-optimizer 0.12.4
@angular-devkit/build-webpack 0.12.4
@angular-devkit/core 7.2.4
@angular-devkit/schematics 7.3.9
@angular/cdk 7.3.1
@angular/cli 7.3.9
@angular/flex-layout 7.0.0-beta.23
@angular/material 7.3.1
@ngtools/webpack 7.2.4
@schematics/angular 7.3.9
@schematics/update 0.13.9
rxjs 6.5.2
typescript 3.2.4
webpack 4.28.4

my karma.conf.js:
_ _ ____ _ ___


const tags = process.argv.filter(arg => {
return (arg.endsWith('.component') || arg.endsWith('.service'))
})

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-spec-reporter'),
require('karma-coverage-istanbul-reporter'),
require('@angular-devkit/build-angular/plugins/karma'),

    ],
    mime: {
        'text/x-typescript': ['ts', 'tsx']
    },
    client: {
        args: [tags],
        clearContext: false // leave Jasmine Spec Runner output visible in browser!
    },
    coverageIstanbulReporter: {
        dir: require('path').join(__dirname, 'coverage'),
        reports: ['html', 'lcovonly'],
        fixWebpackSourcePaths: true
    },

    // reporters: ['progress', 'kjhtml', 'spec'],
    reporters: ['kjhtml', 'spec'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_DEBUG,
    autoWatch: true,
    browsers: ['Chrome'],
    singleRun: false,
    captureTimeout: 240000,
    browserDisconnectTimeout: 10000,
    browserDisconnectTolerance: 3,
    browserNoActivityTimeout: 60000,
});
};

**PS: removed files and the preprocessor configs from the karma.conf.js completely. This is all defined in the angular.json and should be handled automatically by the @anguler-devkit karma plugin**

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 var __karma__: any
declare var require: any

const tags = __karma__.config.args[0]

// Prevent Karma from running prematurely.
__karma__.loaded = function () { }

getTestBed().initTestEnvironment(
    BrowserDynamicTestingModule,
    platformBrowserDynamicTesting()
)

let filterRegex
if (tags.length) {
    const strTags = tags.join('|')
    filterRegex = new RegExp(`(${strTags})\\.spec\\.ts$`) // SPECIFIC TEST SUITES AS PASSED TO ... npm test filter-list.component filter-list.service
} else {
    filterRegex = /\.spec\.ts$/ // ALL TEST SUITES
}

// filterRegex = /(filter-list.component|filter-list.service)\.spec\.ts$/ // OR YOU CAN UNCOMMENT AND EDIT THIS LINE

const context = require.context('./', true, /\.spec\.ts$/)
const specFiles = context.keys().filter(path => filterRegex.test(path))

if (specFiles.length) {
    specFiles.map(context)
    __karma__.start()
} else {
    console.warn(`Could not find any test suites for ${tags.join(', ')}`)
}



i expect the pass my all test which was working when i was using angular 5 but now i am getting "no spec found"

1 个答案:

答案 0 :(得分:1)

在进行迁移时,您应该检查旧应用程序的文件,并将其与带有ng new的新近创建的angular 7应用程序进行比较。

这是100%的错误配置,原因是文件名错误或配置架构错误。没有所有tsconfig.jsonangular.json文件,真的无法提供更多帮助。