gpt4 book ai didi

unit-testing - 无法在 PhantomJS 浏览器中执行 karma 单元测试

转载 作者:行者123 更新时间:2023-12-04 12:08:09 26 4
gpt4 key购买 nike

在 grunt 中,我们通过以下方式在 PhantomJs 中执行单元测试。

karma: {
options: {
configFile: 'karma.conf.js',
runnerPort: 9876,
browsers: ['Chrome']
},
unitTestDev: {
browsers: ['PhantomJS'],
singleRun: true
}
}

grunt.registerTask('unitTest',['karma:unitTestDev']);

这是 karma.conf.js 文件。
    // Karma configuration
// Generated on Thu Apr 03 2014 13:44:39 GMT-0500 (Central Daylight Time)

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: [
{pattern: 'dist/lib/jquery/dist/jquery.js', watched: false, served: true, included: true},
{pattern: 'dist/lib/jasmine-jquery/lib/jasmine-jquery.js', watched: false, served: true, included: true},
{pattern: 'dist/lib/angular/angular.js', watched: false, served: true, included: true},
{pattern: 'dist/lib/angular-route/angular-route.min.js', watched: false, served: true, included: true},
{pattern: 'dist/lib/angular-mocks/angular-mocks.js', watched: false, served: true, included: true},

{pattern: 'dist/main.min.js', watched: false, served: true, included: true},

{pattern: 'public/test/unit/**/*Spec.js', watched: false, served: true, included: true},

// fixtures
{pattern: 'public/test/mock/**/*.json', watched: true, served: true, included: false}
],


// 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', 'html', 'junit', 'story'],

htmlReporter: {
outputDir: 'build/unit-test/html-report',
templatePath: 'node_modules/karma-html-reporter/jasmine_template.html'
},

junitReporter: {
outputFile: 'build/unit-test/junit-report/test-results.xml',
suite: ''
},

// 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: ['Chrome'],

// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: false
});
};

当执行“karma:unitTestDev”命令时,出现以下错误。
Running "karma:unitTestDev" (karma) task
INFO [karma]: Karma v0.11.14 server started at http://localhost:9876/
INFO [launcher]: Starting browser PhantomJS
ERROR [launcher]: Cannot start PhantomJS
Error: spawn OK
INFO [launcher]: Trying to start PhantomJS again (1/2).
ERROR [launcher]: Cannot start PhantomJS
Error: spawn OK
INFO [launcher]: Trying to start PhantomJS again (2/2).
ERROR [launcher]: Cannot start PhantomJS
Error: spawn OK
ERROR [launcher]: PhantomJS failed 2 times (cannot start). Giving up.
Warning: Task "karma:unitTestDev" failed. Use --force to continue.

Aborted due to warnings.

我们在 Windows 和 Linux 操作系统中遇到了上述问题。在 Mac 中,karma 在 PhantomJS 上成功执行测试。

在 Windows 中,如果我们在 Chrome 浏览器中执行上述测试,而不是 PhantomJS,那么 karma 会成功执行所有单元测试。

以下是 Package.json 中与 karma 相关的依赖项
"phantomjs": "~1.9.7-3",
"karma": "~0.12.3",
"karma-chrome-launcher": "~0.1.3",
"karma-jasmine": "~0.1.5",
"karma-html-reporter": "~0.2.3",
"karma-junit-reporter": "~0.2.1",
"grunt-karma": "~0.8.2",
"karma-phantomjs-launcher": "~0.1.3"

让我知道 PhantomJS 的 karma 或咕噜声中是否有任何已知问题。

最佳答案

“Karma-phantomjs-launcher”v0.1.3 存在问题。将其更改为 v0.1.4,然后它就可以正常工作了。

关于unit-testing - 无法在 PhantomJS 浏览器中执行 karma 单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23001125/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com