问题
My team is working on moving from Grunt to Gulp. I have a Grunt test task that works fine, but when I try to run the tests (using gulp-karma) I get an error that says "ReferenceError: Can't find variable: module"
I Googled and found a lot of posts saying to check the location of my angular-mocks.js file, and it's in the correct space (my Grunt task for the same code does work).
To verify that it wasn't something weird in my code a spun up a new yo angular app and was able to replicate the error.
My guess is that I'm missing a configuration value or a step or something. Any advice?
Thanks.
回答1:
When you are running gulp tests
, gulp-karma is looking for files within gulpfile.js
, not karma.conf.js
as you have defined. Remember, when you return gulp.src(tests)
, you are passing in the array of files needed for the tests to pass.
To fix your problem, in your gulpfile.js
, update your tests
array to include your bower files, app files, and test files:
var tests = [
'bower_components/angular/angular.js',
'bower_components/angular-mocks/angular-mocks.js',
'bower_components/angular-animate/angular-animate.js',
'bower_components/angular-cookies/angular-cookies.js',
'bower_components/angular-resource/angular-resource.js',
'bower_components/angular-route/angular-route.js',
'bower_components/angular-sanitize/angular-sanitize.js',
'bower_components/angular-touch/angular-touch.js',
'app/scripts/app.js',
'app/scripts/**/*.js',
'test/mock/**/*.js',
'test/spec/**/*.js'
];
You can delete the files
array in karma.conf.js
as they are not needed.
(p.s. I also made a reference to app.js as app/scripts/**/*.js
would not have matched app.js which needs to be loaded in first)
回答2:
The answer by @mcranston18 will absolutely work. But as an alternative option, gulp-karma
is currently an unnecessary plugin. Part of the advantage of gulp
is that you don't have to use a plugin for everything under the sun. Instead, you can wire into karma
directly.
var gulp = require('gulp');
var karma = require('karma').server;
gulp.task('tests', function(done) {
return karma.start({
configFile: __dirname + '/test/karma.conf.js',
singleRun: true
}, done);
});
来源:https://stackoverflow.com/questions/27365203/getting-module-not-defined-error-when-using-gulp-to-run-karma-tests