ReferenceError: Can't find variable: require at

匿名 (未验证) 提交于 2019-12-03 01:14:02

问题:

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

ReferenceError: Can't find variable: require at

whenever I run my 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 a require just fine, but when I include a require in a test, like so, I get the require error.

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

回答1:

I had the same problem. Just install this node package and add this line to your Gruntfile and require should 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')         }       }     }, 


回答2:

The solution that @user3741597 is suggesting might work, but it is a bit of a backwards solution.

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

This post may help as well.



回答3:

Leading on from the solution by @justin-helmer, 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 of grunt config using this:

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


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