ãã¾ã«ããã¨å¿ãã¦ãã®ã§ã¡ã¢ã
Karmaã¯node.jsãã¼ã¹ã®ãã¹ãã©ã³ãã¼ã§ãã¿ã¼ããã«ãããã¹ããå®è¡ã§ãã¦ãè¤æ°ã®ãã©ã¦ã¶ãç«ã¡ä¸ãã¦ãã¹ããå®è¡ãããããã¡ã¤ã«ã®å¤æ´ãç£è¦ãã¦èªåã§ãã¹ããå®è¡ããããã§ãã¾ãã
RequireJSã使ã£ã¦ãJavascriptã«å¯¾ãã¦ããã¹ããã¬ã¼ã ã¯ã¼ã¯ã¨ãã¦mochaãã¢ãµã¼ã·ã§ã³ã©ã¤ãã©ãªã¨ãã¦expectã使ã£ããã¹ããæ¸ããKarmaã§å®è¡ãã¾ãã
â»2014/3/12è¿½è¨ ä½¿ç¨ãããã¼ã¸ã§ã³ã¯ä»¥ä¸ã
- node v0.10.25
- npm 1.3.24
- karma 0.12.0
追è¨ããã¾ã§ã
æçµçã«ã¯ãããªæãã«ã
$ tree . |-- karma.conf.js |-- package.json |-- src | `-- js | |-- app.js | |-- lib | | |-- require.js | | `-- underscore.js | `-- require.config.js `-- test `-- js |-- appSpec.js `-- require.config.js
ã³ã¼ãã®æºå
ãããã¯ãã³ã¼ããsrc/app.jsãRequireJSã¨Underscore.jsã¯ãã¦ã³ãã¼ããã¦src/libã¸ãç¡çç¢çUnderscore.jsã使ã£ããããã¯ãã³ã¼ããç¨æãã¦ã¿ã ã
// src/app.js define(['underscore'], function(){ return { max: function(a, b, c){ return _.max([a, b, c]); } }; });
ãã¹ãã³ã¼ããtest/appSpec.jsã
// test/appSpec.js define(['app'], function(app){ describe('appã¢ã¸ã¥ã¼ã«ã®ãã¹ã', function(){ it('maxã¡ã½ããã®ãã¹ã', function(){ expect(app.max(1,2,3)).to.be(3); }); }); });
package.jsonãç¨æã
{ "name": "karma-sample", "version": "0.0.1" }
Karmaãã¤ã³ã¹ãã¼ã«
Karmaãã¤ã³ã¹ãã¼ã«ããã
$ npm install -g karma-cli $ npm install --save-dev karma
Karmaã®è¨å®ãã¡ã¤ã«ãä½æ
karma initã§è¨å®ãã¡ã¤ã«ãçæ
Karma initãå®è¡ãã¦è³ªåã«çãã¦ãããããããã¨karma.conf.jsãçæãããã
$ karma init Which testing framework do you want to use ? Press tab to list possible options. Enter to move to the next question. > mocha Do you want to use Require.js ? This will add Require.js plugin. Press tab to list possible options. Enter to move to the next question. > yes Do you want to capture a browser automatically ? Press tab to list possible options. Enter empty string to move to the next question. > Chrome >_ What is the location of your source and test files ? You can use glob patterns, eg. "js/*.js" or "test/**/*Spec.js". Enter empty string to move to the next question. > src/**/*.js > test/**/*Spec.js >_ Should any of the files included by the previous patterns be excluded ? You can use glob patterns, eg. "**/*.swp". Enter empty string to move to the next question. > src/require.config.js WARN [init]: There is no file matching this pattern. >_ Which files do you want to include with <script> tag ? This should be a script that bootstraps your test by configuring Require.js and kicking __karma__.start(), probably your test-main.js file. Enter empty string to move to the next question. > test/require.config.js WARN [init]: There is no file matching this pattern. > Do you want Karma to watch all the files and run the tests on change ? Press tab to list possible options. > yes
Karmaã®ãã©ã°ã¤ã³ãã¤ã³ã¹ãã¼ã«
Karmaã§ãrequirejsãmochaãexpectã使ãããã®ãã©ã°ã¤ã³ãã¤ã³ã¹ãã¼ã«ããã
$ npm install --save-dev karma-expect
çæãããkarma.conf.jsã確èª
çæãããkarma.conf.jsãä¸èº«ãè¦ãã¨ããããã¨è¨å®é
ç®ãã
frameworks
ã¯ãkarmaã使ããã©ã°ã¤ã³ãæå®ãããmochaã¨requirejsãè¨è¿°ããã¦ããããexpectãè¨è¿°ããã¦ããªãã®ã§è¿½è¨ããã
// frameworks to use frameworks: ['mocha', 'requirejs', 'expect'],
files
ã«ã¯ããã¹ãã§ä½¿ãjsãã¡ã¤ã«ãæå®ãããkarma init
æã«æå®ãããã¿ã¼ã³ã¨test/require.config.jsãè¨è¿°ããã¦ããã
// list of files / patterns to load in the browser files: [ 'test/require.config.js', {pattern: 'src/**/*.js', included: false}, {pattern: 'test/**/*Spec.js', included: false} ],
'test/require.config.js'
ã®ããã«åç´ã«ãã¡ã¤ã«ï¼ã¯ã¤ã«ãã«ã¼ãå¯ï¼ãæå®ããæ¹æ³ã¨ã{pattern: 'src/**/*.js', included: false}
ã®ããã«JSONã§ãªãã·ã§ã³ã¨ä½µãã¦æå®ãããã¨ãã§ããã
src/**/*.js
ã¨test/**/*Spec.js
ã«ã¯ãincluded: false
ã¨ãããªãã·ã§ã³ãã¤ãã¦ããããããã¯karmaããã©ã¦ã¶ãç«ã¡ä¸ããã¨ãã«scriptã¿ã°ã§jsãã¡ã¤ã«ãèªã¿è¾¼ã¾ãªãããã®æå®ãscriptã¿ã°ã§ã¯ãªãRequireJSã使ã£ã¦jsãã¡ã¤ã«èªã¿è¾¼ãã®ã§ãã®ãªãã·ã§ã³ãä»ä¸ãããã
exclude
ã«ã¯ããã¹ãå®è¡æã«èªã¿è¾¼ã¿ãããªããã¡ã¤ã«ãæå®ãkarma init
æã«æå®ãããã¿ã¼ã³ãè¨è¿°ãããããã¹ãç¨ã®require.config.jsã使ãããããããã¯ãã³ã¼ãç¨ã®require.config.jsãæå®ãã¦ãã¹ãå®è¡æã«ã¯èªã¿è¾¼ã¾ãªãããã«ããã
// list of files to exclude exclude: [ 'src/require.config.js' ],
ä»ã®è¨å®é ç®ã¯ãRequireJSã使ããªãå ´åã¨ãã£ããã
ãã¹ãç¨ã®require.configãè¨å®
ãã¹ãå®è¡æã¯ãããã¯ãã³ã¼ãç¨ã®require.configã使ããªãã代ããã«ãã¹ãç¨ã®require.configãè¨å®ããã
ãããã¯ãã³ã¼ãç¨ã®require.configã¨ã®éãã¯ãbaseUrlã¨depsã¨callbackã
// test/require.config.js var tests = []; for (var file in window.__karma__.files) { if (window.__karma__.files.hasOwnProperty(file)) { if (/Spec\.js$/.test(file)) { tests.push(file); } } } require.config({ baseUrl: "/base/src", paths: { "underscore": "lib/underscore" }, shim: { "underscore": { exports: "_" } }, deps: tests, callback: window.__karma__.start });
baseUrl
- /baseã§å§ã¾ããã¹ãè¨å®ãããkarmaãå®è¡ããã¨ãhttp://localhost:9876/base ãkarma.conf.jsãåå¨ãããã£ã¬ã¯ããªã«ãªãããã®ããããããã¯ãã³ã¼ãã®
baseUrl
ã¨åããã£ã¬ã¯ããªãæãããã«ãbaseUrl
ã/base/srcã«è¨å®ããã
- /baseã§å§ã¾ããã¹ãè¨å®ãããkarmaãå®è¡ããã¨ãhttp://localhost:9876/base ãkarma.conf.jsãåå¨ãããã£ã¬ã¯ããªã«ãªãããã®ããããããã¯ãã³ã¼ãã®
deps
- ãã¹ãã³ã¼ããæå®ããã
window.__karma__.files
ã«karma.conf.jsã®filesã§æå®ãããã¡ã¤ã«ãæ ¼ç´ããã¦ããã®ã§ããã¹ãã³ã¼ãï¼ãã¡ã¤ã«åãSpec.jsã§çµãã£ã¦ãããã¡ã¤ã«ï¼ã®ã¿ãtests
é åã«æ ¼ç´ãã¦ãdeps
ã«æå®ããã
- ãã¹ãã³ã¼ããæå®ããã
callback
window.__karma__.start
ãæå®ãããããã§ãdeps
ã«è¨è¿°ãããã¹ãã³ã¼ããå ¨ã¦ãã©ã¦ã¶ã«èªã¿è¾¼ã¾ããå¾ã«ãwindow.__karma__.start
ãå®è¡ãããã¹ããåä½ããã
karmaãå®è¡
ããã§æºåãã§ããã®ã§ããã¨ã¯ä»¥ä¸ã®ã³ãã³ãã§å®è¡ã
$ karma start karma.conf.js
Gruntã§karmaå®è¡
gruntããkarmaãå®è¡ã§ããã¨ä»ã®ã¿ã¹ã¯ã¨çµã¿åããããã§ãã¦ä¾¿å©ãã¨ãããã¨ã§ãgruntã¨grunt-karmaãã¤ã³ã¹ãã¼ã«ã
$ npm install --save-dev grunt grunt-karma
karmaã¿ã¹ã¯ãå®ç¾©ãã¦ã
// Gruntfile.js module.exports = function(grunt){ grunt.initConfig({ karma: { unit: { configFile: 'karma.conf.js' } } }); grunt.loadNpmTasks('grunt-karma'); };
å®è¡ããã
$ grunt karma