karma.js在基本的“;因果报应;设置

karma.js not returning any results following basic "karma init" setup

本文关键字:因果报应 设置 js karma      更新时间:2023-09-26

我正在学习egghead.io的"因果报应入门"教程https://egghead.io/lessons/unit-testing-introduction-to-karma,并遵循以下内容(在Windows上):

> npm install --g karma-cli
> npm install karma karma-jasmine karma-chrome-launcher --save-dev
karma init

遵循初始karma配置的默认配置,然后在当前文件夹中创建一个test.spec.js文件,代码如下:

describe('Test', function() {
    it('It should be true', function() { expect(true).toBe(true); });
});

最后,我在karma.conf.js文件中添加了这样的文件:

files: [
      'test.spec.js'
    ],

但是,在运行时

karma start karma.conf.js

我只得到一个:

25 11 2015 16:35:35.551:INFO [karma]: Karma v0.13.15 server started at http://localhost:9876/

为什么不执行任何测试?此外,打开localhost url(这与在karma.conf.js中将浏览器设置为['chrome']相同)只会显示karma正在运行,chrome处于空闲状态,但不会返回任何测试结果。

edit:添加了karma.conf.js

// Karma configuration
// Generated on Wed Nov 25 2015 16:19:43 GMT-0600 (Central Standard Time (Mexico))
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: [
      '*spec.js'
    ],

    // list of files 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: false,

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

    // Continuous Integration mode
    // if true, Karma captures browsers, runs the tests and exits
    singleRun: false,
    // Concurrency level
    // how many browser should be started simultanous
    concurrency: Infinity
  })
}

edit2:添加了package.json

{
  "name": "karma-test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "karma start karma.conf.js"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "jasmine-core": "^2.3.4",
    "karma": "^0.13.15",
    "karma-chrome-launcher": "^0.2.1",
    "karma-jasmine": "^0.3.6"
  }
}

edit3:找到了一个解决方案。查看我自己的回复

您需要为Karma设置browsers : ['Chrome'],以便自动运行测试。

如果你不这样做,你需要在浏览器中打开它:http://localhost:9876/以捕获打开的浏览器。

找到了解决方案,或者有点。我似乎不得不按下localhost:9876url上的大"DEBUG"按钮,打开chrome控制台并在那里找到结果。这非常令人困惑,我希望业力医生告诉我。现在我们只需要一种在窗口控制台中显示结果的方法

编辑

修复了它……我很惊讶这能修复它。不管怎样,我们终于在karma.conf.js和打开中将autoWatch设置为true(它是false),得到了正确的输出http://localhost:9876.我仍然很困惑,我使用了不需要浏览器的无头测试,但我稍后会对此进行研究。我猜这是为了让调试更容易。