将 Karma 设置为 运行 使用 jasmine、requirejs 和 react 进行测试
Settings Karma to run tests with jasmine, requirejs and react
我正在努力创建我自己的 bootstrap project which includes RequireJS for modules structure, Backbone for routing and simple static views, ReactJS for components. The project works fine, but I want to add unit-testing support to cover my units of code in future projects. I'm trying to add Karma as a test runner with Jasmine and RequireJS support in unit tests (then I'll add TestUtils 用于组件测试)。
目前我在使用 Karma 时遇到了一些问题...它对我不起作用...
我创建了 Gruntfile.js 并配置了 karma:
karma: {
unit: {
configFile: 'karma.config.js'
}
}
我还添加了karma.config.js
file with configurations. And also I added the file with RequireJS configurations for tests test-main.js
但是我在尝试使用命令 grunt test
:
进行 运行 测试时遇到了麻烦
grunt test
Running "karma:unit" (karma) task
14 12 2015 15:06:17.594:WARN [karma]: No captured browser, open http://localhost:9876/
14 12 2015 15:06:17.634:INFO [karma]: Karma v0.13.15 server started at http://localhost:9876/
14 12 2015 15:06:17.644:INFO [launcher]: Starting browser PhantomJS
14 12 2015 15:06:18.849:INFO [PhantomJS 1.9.8 (Windows 7 0.0.0)]: Connected on socket Gvi8xqI0lfbPEEOGAAAA with id 60807377
PhantomJS 1.9.8 (Windows 7 0.0.0): Executed 0 of 0 ERROR (0.013 secs / 0 secs)
并且在此消息上程序卡住了...有没有人提供有用的建议???
PS:我使用本教程将 RequireJS 与 Karma 测试结合使用:http://karma-runner.github.io/0.8/plus/RequireJS.html
添加到karma.config.js
files: [
{pattern: 'bower_components/**/*.js', included: false},
{pattern: 'scripts/**/*.js', included: false},
{pattern: 'spec/**/*Spec.js', included: false},
'spec/test-main.js'
],
exclude: [
'scripts/main.js'
],
frameworks: ['jasmine', 'requirejs'],
plugins: ['karma-phantomjs-launcher', 'karma-requirejs', 'karma-jasmine'],
运行 来自命令行:
npm install requirejs --save-dev
在spec/test-main.js
var pathToModule = function (path) {
console.log(path);
return path.replace(/^spec\//, '../spec/').replace(/\.(js)$/, '');
};
...
allTestFiles.push(pathToModule(normalizedTestModule));
...
baseUrl: '/base/scripts',
并移除
urlArgs: ...
我正在努力创建我自己的 bootstrap project which includes RequireJS for modules structure, Backbone for routing and simple static views, ReactJS for components. The project works fine, but I want to add unit-testing support to cover my units of code in future projects. I'm trying to add Karma as a test runner with Jasmine and RequireJS support in unit tests (then I'll add TestUtils 用于组件测试)。
目前我在使用 Karma 时遇到了一些问题...它对我不起作用... 我创建了 Gruntfile.js 并配置了 karma:
karma: {
unit: {
configFile: 'karma.config.js'
}
}
我还添加了karma.config.js
file with configurations. And also I added the file with RequireJS configurations for tests test-main.js
但是我在尝试使用命令 grunt test
:
grunt test Running "karma:unit" (karma) task 14 12 2015 15:06:17.594:WARN [karma]: No captured browser, open http://localhost:9876/ 14 12 2015 15:06:17.634:INFO [karma]: Karma v0.13.15 server started at http://localhost:9876/ 14 12 2015 15:06:17.644:INFO [launcher]: Starting browser PhantomJS 14 12 2015 15:06:18.849:INFO [PhantomJS 1.9.8 (Windows 7 0.0.0)]: Connected on socket Gvi8xqI0lfbPEEOGAAAA with id 60807377 PhantomJS 1.9.8 (Windows 7 0.0.0): Executed 0 of 0 ERROR (0.013 secs / 0 secs)
并且在此消息上程序卡住了...有没有人提供有用的建议???
PS:我使用本教程将 RequireJS 与 Karma 测试结合使用:http://karma-runner.github.io/0.8/plus/RequireJS.html
添加到karma.config.js
files: [
{pattern: 'bower_components/**/*.js', included: false},
{pattern: 'scripts/**/*.js', included: false},
{pattern: 'spec/**/*Spec.js', included: false},
'spec/test-main.js'
],
exclude: [
'scripts/main.js'
],
frameworks: ['jasmine', 'requirejs'],
plugins: ['karma-phantomjs-launcher', 'karma-requirejs', 'karma-jasmine'],
运行 来自命令行:
npm install requirejs --save-dev
在spec/test-main.js
var pathToModule = function (path) {
console.log(path);
return path.replace(/^spec\//, '../spec/').replace(/\.(js)$/, '');
};
...
allTestFiles.push(pathToModule(normalizedTestModule));
...
baseUrl: '/base/scripts',
并移除
urlArgs: ...