ReferenceError: Cannot find variable: require at

I have a question about using jasmine with Grunt. I keep getting the error

ReferenceError: cannot find variable: require at

whenever I run jasmine tests. Here is my jasmine entry for my Gruntfile.js:

jasmine: { js: { src: jsFiles, options: { specs: 'tests/*_spec.js', helpers: 'tests/helpers/*', vendor: 'vendor/*' } } }, 

I can run a dummy test without the need for a fine, but when I include the requirement in the test, for example, I get the error I want.

 var testD = require('../src/events_to_actions'); describe("events_to_actions", function() { it("is dummy test", function() { expect(true).toEqual(true); }); }); 
+6
source share
3 answers

I had the same problem. Just install this node package and add this line to your Grunt file and you need to start working again.

https://github.com/cloudchen/grunt-template-jasmine-requirejs

 jasmine: { js: { src: jsFiles, options: { specs: 'tests/*_spec.js', helpers: 'tests/helpers/*', vendor: 'vendor/*', template: require('grunt-template-jasmine-requirejs') } } }, 
+7
source

The solution suggested by @ user3741597 may work, but this is a little inverse solution.

"grunt-template-jasmine-requirejs" was written for RequireJS, which is the AMD loader. On the other hand, you use CommonJS syntax. If you want to continue using "grunt-contrib-jasmine", you need to find the CommonJS template or use the information that Jasmine has node support inline and use a different approach.

This post may also help.

+4
source

Following @ justin-helmer's solution , there is a template that allows you to use require calls (CommonJS syntax) with grunt-contrib-jasmine:

https://www.npmjs.com/package/grunt-template-jasmine-nml

An example grunt configuration using this:

 jasmine: { options: { template: require('grunt-template-jasmine-nml'), helpers: 'spec/helpers/**/*.js', specs: 'spec/**/*.spec.js' } } 
0
source

All Articles