【发布时间】:2019-11-10 11:07:56
【问题描述】:
我尝试使用 Karma 运行一小组 Jasmine 测试。非常感谢任何帮助。
如果我运行命令“jasmine”,我可以很好地运行测试。但是,当我尝试使用 Karma (karma start --singleRun=true) 时,浏览器会打开,但在 Karma 最终超时之前没有任何反应:
26 06 2019 14:40:24.431:INFO [karma]: Delaying execution, these browsers are not ready: Chrome 75.0.3770 (Windows 10.0.0)
26 06 2019 14:42:01.326:WARN [Chrome 75.0.3770 (Windows 10.0.0)]: Disconnected (0 times), because no message in 160000 ms.
26 06 2019 14:42:01.327:DEBUG [Chrome 75.0.3770 (Windows 10.0.0)]: CONFIGURING -> DISCONNECTED
Chrome 75.0.3770 (Windows 10.0.0) ERROR
Disconnected, because no message in 160000 ms.
26 06 2019 14:42:01.330:DEBUG [launcher]: CAPTURED -> BEING_KILLED
26 06 2019 14:42:01.332:DEBUG [launcher]: BEING_KILLED -> BEING_FORCE_KILLED
26 06 2019 14:42:01.333:WARN [karma]: No captured browser, open http://localhost:9876/
Chrome 75.0.3770 (Windows 10.0.0): Executed 0 of 0 DISCONNECTED (2 mins 40.011 secs / 0 secs)
如果我改为使用 --singleRun=false 运行并在我的测试中设置断点,然后从新的命令提示符运行“karma run karma.conf.js”,我注意到断点在实现回调中 因为我的测试永远不会受到打击。其他断点也能正常命中。
例如在下面的测试中:
describe(">String Utils", function() {
it("should be able to lower case a string",function() {
expect(utils.toLowerCase).toBeDefined();
expect(utils.toLowerCase("HELLO WORLD")).toEqual("hello world");
});
“describe”或“it”函数上的断点将被命中。但是任何一个“预期”上的断点都不会被命中。
我已进入“it”功能,看看在尝试设置 jasmine 规范时是否出现错误,但一切似乎都很好。
但是,Karma 从不调用实现回调。 And the browser continually stays "idle"
这是我的 karma.conf.js。下面是我的 spec-bundle.js,下面是我的 webpack。
如果你还有什么想看的,我可以发布或者你可以在我的 github repo 上查看:https://github.com/webgirlwonder/karma-jasmine-test
// Karma configuration
// Generated on Fri May 17 2019 10:37:04 GMT-0500 (Central Daylight Time)
var webpackConfig = require('./webpack.config.js');
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','requirejs'],
// list of files / patterns to load in the browser
files: [
'spec-bundle.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: {
// '*.js': [ 'webpack' ],
// 'spec/*Spec.js': ['webpack' ]
// },
preprocessors: {
'spec-bundle.js': ['webpack' ]
},
webpack: webpackConfig,
// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['spec'],
// 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_DEBUG,
/* // 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: true, */
// Concurrency level
// how many browser should be started simultaneous
concurrency: Infinity,
captureTimeout: 160000,
browserNoActivityTimeout: 160000,
})
}
spec-bundle.js
/*
* Create a context for all tests files below the src folder and all sub-folders.
*/
const context = require.context('./spec/', true, /\Spec\.js$/);
/*
* For each file, call the context function that will require the file and load it up here.
*/
context.keys().forEach(context);
网络包
const config = {
"mode": "development",
"entry": "./spec/MyJSUtilities.spec.js",
"target": "node",
"output": {
"path": __dirname+'/static',
"filename": "[name].[chunkhash:8].js"
}
}
module.exports = config;
【问题讨论】:
-
提出拉取请求。
标签: javascript unit-testing jasmine karma-jasmine karma-runner