首页 > 解决方案 > Karma 模式与 Angular 项目中的任何文件都不匹配

问题描述

问题

我正在尝试在实践 Angular 项目中使用 Karma 和 Jasmine 进行一些测试。当我尝试使用Karma start命令行中的命令运行 Karma 测试时,会打开一个浏览器(在本例中为 Chrome),但不显示任何测试。它仅显示 Karma 版本、调试按钮和指示 Chrome 空闲的消息(见下图)。在命令行中,Karma 给出警告

All files matched by "D:/Documents/CSCI_Project/test-app/*.js" were excluded or matched by prior matchers.

它还给出了表单的多个警告

Pattern "D:/Documents/CSCI_Project/test/test-app/testPractice.spec.js" does not match any file.

测试文件包含旨在始终通过的基本测试。文件的绝对路径是D:\Documents\CSCI_Project\test-app\test\testPractice.spec.ts

采取的步骤

根据错误,Karma 似乎无法使用配置文件 karma.config.js 的“文件”部分中给出的模式找到我的测试文件。但是,我尝试了许多模式,包括列出确切的相对路径并确保该模式的警告中显示的路径是文件的绝对路径。

我认为打字稿文件会自动转换为 javascript 文件,但我尝试使用以.ts代替结尾的模式.js,这导致了业力警告Unable to determine file type from the file extension, defaulting to js.

在这一点上,我想不出还有什么问题。我对 Karma、Jasmine 或 Angular 不是很熟悉。

代码/图片

karma.conf.js 文件

// Generated on Sat Feb 06 2021 12:10:57 GMT-0500 (Eastern Standard Time)

module.exports = function(config) {
  config.set({

    // base path that will be used to resolve all patterns (eg. files, exclude)
    basePath: '',


    // frameworks to use
    // available frameworks: https://npmjs.org/browse/keyword/karma-adapter
    frameworks: ['jasmine'],


    // list of files / patterns to load in the browser
    files: [
      '*.js',
      'test/**/*.js',
      'test/*.js',
      'test/testPractice.js',
      'test/testPractice.spec.js',
    ],


    // list of files / patterns to exclude
    exclude: [
    ],


    // preprocess matching files before serving them to the browser
    // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
    preprocessors: {
    },


    // test results reporter to use
    // possible values: 'dots', 'progress'
    // available reporters: https://npmjs.org/browse/keyword/karma-reporter
    reporters: ['progress'],


    // web server port
    port: 9876,


    // enable / disable colors in the output (reporters and logs)
    colors: true,


    // level of logging
    // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
    logLevel: config.LOG_INFO,


    // enable / disable watching file and executing tests whenever any file changes
    autoWatch: true,


    // start these browsers
    // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
    browsers: ['Chrome'],


    // Continuous Integration mode
    // if true, Karma captures browsers, runs the tests and exits
    singleRun: false,

    // Concurrency level
    // how many browser should be started simultaneous
    concurrency: Infinity
  })
}

Karma 打开的 Chrome 窗口: 图片链接

带有 Karma 警告的命令行: 图片链接

测试文件:testPractice.ts

describe("Practice Test", () =>{
    it("Should return true", ()=>{
        var test = true;
        expect(test).toBe(true);
    });
});

标签: angulartypescriptkarma-jasmine

解决方案


关于“不匹配任何文件”的警告是有道理的,因为模式test/*.js已经与文件test/testPractice.js和匹配test/testPractice.spec.js,所以后面的模式是多余的。

我不确定为什么测试实际上没有运行——您是否尝试过添加kjhtml记者(npm install karma-jasmine-html-reporter)?我发现它可以更好地准确传达测试与个人状态一起运行的内容。


推荐阅读