【问题标题】:Jasmine: "Incomplete: No specs found" in Angular Typescript projectJasmine:Angular Typescript 项目中的“不完整:未找到规范”
【发布时间】:2020-03-31 11:02:49
【问题描述】:

出于某种我无法理解的原因,Karma 说 Jasmine 找不到我的任何测试规范。我正在使用 Angular 9、Typescript 和 ng test 来运行测试。我还运行 jasmine init 来创建 jasmine.json 配置文件。我已经尝试了几次配置更改,创建了一个虚拟测试 javascript 文件等等,但我仍然收到相同的“未找到规范”消息。这真的令人沮丧,我敢肯定我只是在这里忽略了一些明显的东西。

测试文件夹结构:

spec
    services
        address.service.spec.ts
        login.service.spec.ts
        ...
     support
         jasmine.json

jasmine.json:

{
  "spec_dir": "spec",
  "spec_files": [
  "**/*[sS]pec.js", // tried with "**/*[sS]pec.ts"
  "services/*[sS]pec.js" // tried with "services/*[sS]pec.ts"
  ],
  "helpers": [
  "helpers/**/*.js" // tried with "helpers/**/*.ts"
  ],
  "stopSpecOnExpectationFailure": false,
  "random": false
}

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'),
        ],
        client: {
            clearContext: false, // leave Jasmine Spec Runner output visible in browser
        },
        coverageIstanbulReporter: {
            dir: require('path').join(
                __dirname,
                './coverage/censored',
            ),
            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,
    });
};

【问题讨论】:

  • 由于Angular 9还没有发布,我建议你在github上提出一个问题,因为更多的人可能会遇到这种情况,需要核心团队来解决。
  • 我可能应该验证它是否可以预先与 Angular 8 一起使用。

标签: angular typescript testing jasmine karma-runner


【解决方案1】:

我今天遇到了这个错误,对我来说是因为我的 .ts 文件中有语法级别的错误,所以它们无法真正编译。

在上述情况下,我认为测试不应该开始(而是显示编译错误)。

但无论出于何种原因,测试以某种方式开始,Jasmine 失败并显示“No specs found”日志。

【讨论】:

    【解决方案2】:

    在我的情况下,我在运行 ng test 时在终端中看到了一些错误,但是当浏览器打开时显示没有找到规范。

    项目错误

    这是错误,因为我在此之后修复了此错误,它显示了所有规范以及执行的测试用例的结果

    【讨论】:

      【解决方案3】:

      我将 .spec 文件移动到 /spec 目录后,它找到了测试。

      【讨论】:

        【解决方案4】:

        我遇到了同样的问题,但在我的情况下,我从 angular 9 升级到 angular 10 并且测试停止运行,我得到了很多:

        src/app/xxxxx/basic-info/basic-info.component.spec.ts:391:9 - error TS2532: Object is possibly 'undefined'.
        
        391 this.whatToDelete = ["mumbo", "jumbo"];
        

        所以在“描述”中,我将箭头函数更改为常规函数,因为箭头函数没有自己的“this”

        *

        与常规函数不同,箭头函数没有自己的 this 。 箭头函数中 this 的值始终保持不变 函数的生命周期并且总是绑定到 this 的值 在最近的非箭头父函数中。

        希望这可以帮助你们并节省一些时间

        【讨论】:

          【解决方案5】:

          解决了。一旦我将 .spec 文件移动到 /src 文件夹中,Jasmine 检测到它们没有任何问题。

          【讨论】:

            猜你喜欢
            • 2023-03-03
            • 2021-05-20
            • 1970-01-01
            • 2018-06-10
            • 2022-10-16
            • 2018-04-30
            • 2020-04-13
            • 2018-07-25
            • 1970-01-01
            相关资源
            最近更新 更多