我正在关注 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:找到解决方案。查看我自己的回复
最佳答案
您需要设置浏览器:['Chrome']
,以便 Karma 自动运行您的测试。
如果您不这样做,则需要在浏览器中打开:http://localhost:9876/捕获打开的浏览器。
关于javascript - karma.js 在基本 "karma init"设置后不返回任何结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33927730/