【问题标题】:karma.js not returning any results following basic "karma init" setupkarma.js 在基本的“karma init”设置之后没有返回任何结果
【发布时间】:2016-02-28 21:43:51
【问题描述】:

我正在学习 Egghead.io 的“Karma 简介”教程,在 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 处于空闲状态,但没有返回任何测试结果。

编辑:添加 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:找到解决方案。检查我自己的回复

【问题讨论】:

标签: javascript node.js unit-testing jasmine karma-runner


【解决方案1】:

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

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

【讨论】:

  • 这与在浏览器上手动导航到该 url 相同(我阅读了文档),我们这样做了(我将其包含在问题描述中)。但它仍然没有显示我们想要的结果。找到一个可以解释的答案,检查我自己的回复
【解决方案2】:

找到了解决方案,或者说是。似乎我必须按下 localhost:9876 url 上的大“DEBUG”按钮,打开 chrome 控制台并在那里找到结果。这非常令人困惑,我希望 Karma 文档告诉我。现在我们只需要一种在 windows 控制台中显示结果的方法

编辑

修复它...我很惊讶这怎么能修复它。无论哪种方式,我们最终通过在 karma.conf.js 中将 autoWatch 设置为 true(它是 false)并打开 http://localhost:9876 来获得正确的输出。我仍然很困惑,我认为 karma 使用了不需要浏览器的无头测试,但我稍后会研究。我猜这是为了让调试更容易。

【讨论】:

  • 您在 Karma 配置文件中添加了browsers : ['Chrome']
  • 不,我刚刚打开 localhost:9876 并获得了 karma UI,尽管设置 ['Chrome'] 本来可以更快。我们希望通过浏览器在节点控制台中获取结果,因为我们计划将其与 Jenkins 一起使用
  • Karma 可以使用无头浏览器,它被称为 PhantomJS 或 PhantomJS2,您可以使用 npm install --save-dev karma-phantomjs-launchernpm install --save-dev karma-phantomjs2-launcher 在项目本地安装。然后简单地添加它:browsers: ['PhantomJS']browsers: ['PhantomJS2']
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-12
  • 1970-01-01
相关资源
最近更新 更多