Karma: can't find variable: export

I wrote a node module that can be used for both the backend and the client

(exports || window).Bar= (function () { return function () { .... } })(); 

Now my karma tests use PhantomJs and complain about the nonexistent export variable

 gulp.task('test', function () { var karma = require('karma').server; karma.start({ autoWatch: false, browsers: [ 'PhantomJS' ], coverageReporter: { type: 'lcovonly' }, frameworks: [ 'jasmine' ], files: [ 'bar.js', 'tests/bar.spec.js' ], junitReporter: { outputFile: 'target/junit.xml' }, preprocessors: { 'app/js/!(lib)/**/*.js': 'coverage' }, reporters: [ 'progress', 'junit', 'coverage' ], singleRun: true }); }); 

The error I get is

 PhantomJS 1.9.7 (Mac OS X) ERROR ReferenceError: Can't find variable: exports 

Is there a way to ignore the export variable in karam / phantomsJs?

+5
source share
1 answer

Typically, a template usually checks to see if the exports variable is defined:

 (function(){ ... var Bar; if (typeof exports !== 'undefined') { Bar = exports; } else { Bar = window.Bar = {}; } })(); 

This pattern is used by Backbone as an example - well, it is technically a bit more complicated in the source code, because it also supports AMD, but the idea is this.

You can also press the test key by passing it as the first argument to the wrapping function:

 (function(exports){ // your code goes here exports.Bar = function(){ ... }; })(typeof exports === 'undefined'? this['mymodule']={}: exports); 

Check out this blog post for more information.

+6
source

Source: https://habr.com/ru/post/1203996/


All Articles