Proper use of karma habit with Jasmine 2

I spent a lot of time trying to debug this, and decided that I would ask. I even created a GitHub repository , but I won’t rely on it, so here it is. I am trying to use CommonJS syntax in a Karma tester using PhantomJS. For my module, I created the simplest thing that I could think of:

exports.returnYes = function() { return "Yes"; }; 

Jasmine test:

 var returnYes = require("../js/returnYes").returnYes; describe("returnYes", function() { it("should return Yes", function() { expect(returnYes()).toBe("Yes"); }); }); 

And, if I do jasmine init , I can run it from the command line using jasmine-npm by simply typing jasmine with the output:

 $ jasmine Started . 1 spec, 0 failures Finished in 0.003 seconds 

Now, to try to get it to work inside karma: I create karma.conf.js with the frameworks: jasmine , commonjs . And, I add commonjs as a preprocessor.

I try to make karma run , and I found that it cannot find global , which is part of getJasmineRequireObj in jasmine.js , where it declares jasmineGlobal = global;

Command line output is a little hard to read, but here it is:

 $ karma run [2015-06-27 17:41:35.266] [DEBUG] config - Loading config /Users/zen/Projects/karma-commonjs-test/karma.conf.js ##teamcity[enteredTheMatrix] ##teamcity[testSuiteStarted nodeId='1' parentNodeId='0' name='karma.conf.js' nodeType='config' locationHint='config:///Users/zen/Projects/karma-commonjs-test/karma.conf.js'] ##teamcity[testSuiteStarted nodeId='2' parentNodeId='1' name='PhantomJS 1.9.8 (Mac OS X 0.0.0)' nodeType='browser'] ##teamcity[testStarted nodeId='3' parentNodeId='2' name='Error' nodeType='browserError'] ##teamcity[testFailed nodeId='3' error='yes' message='ReferenceError: Can|'t find variable: global|nat http://localhost:9876/base/node_modules/jasmine-core/lib/jasmine-core/jasmine.js?68f13ab3f93af5a219b9fe8409f8763b31998bba:27'] ##teamcity[testSuiteFinished nodeId='2'] ##teamcity[testSuiteFinished nodeId='1'] 

For good measure, here are the devDependencies in my packages. json:

 "devDependencies": { "jasmine-core": "^2.3.4", "karma": "^0.12.37", "karma-commonjs": "0.0.13", "karma-jasmine": "^0.3.5", "karma-phantomjs-launcher": "^0.2.0", "phantomjs": "^1.9.17" } 

I am not sure why I cannot find global . Any help would be greatly appreciated! :)

+5
source share
1 answer

It seems that my whole problem came down to a line in karma.conf.js (not shown in my original question:

 preprocessors: { '**/*.js': ['commonjs'] }, 

For some reason, jasmine.js not jasmine.js with the preprocessing of commonjs, and "** / *. Js" says it goes through all the subdirectories (which are probably full), including the node_modules, which has the jasmine kernel /jasmine.js

Therefore, I can either make my preprocessor more specific (best practice):

 preprocessors: { 'spec/*.js': ['commonjs'], 'js/*.js': ['commonjs'] }, 

but as a test, to see if any other files could give me a problem, I tried:

 preprocessors: { '**/!(jasmine).js': ['commonjs'], }, 

And it worked. Bottom line. Do not process jasmine.js through the commonjs preprocessor!

+4
source

All Articles