Getting “module not defined” error when using Gulp to run karma tests

纵然是瞬间 提交于 2020-02-02 04:37:11

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!