【发布时间】: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:找到解决方案。检查我自己的回复
【问题讨论】:
-
你能分享
karma.conf.js吗? -
完成,添加了 package.json
标签: javascript node.js unit-testing jasmine karma-runner