javascript 参考错误:找不到变量:需要在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23449220/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ReferenceError: Can't find variable: require at
提问by worker1138
I have a question about using jasmine with Grunt. I keep getting an error,
我有一个关于在 Grunt 中使用 jasmine 的问题。我不断收到错误,
ReferenceError: Can't find variable: require at
参考错误:找不到变量:需要在
whenever I run my jasmine tests. Here is my jasmine entry for my Gruntfile.js :
每当我运行茉莉花测试时。这是我的 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.
我可以在没有 require 的情况下运行一个虚拟测试,但是当我在测试中包含一个 require 时,像这样,我得到了 require 错误。
var testD = require('../src/events_to_actions');
describe("events_to_actions", function() {
it("is dummy test", function() {
expect(true).toEqual(true);
});
});
回答by user3741597
I had the same problem. Just install this node package and add this line to your Gruntfile and require should start working again.
我有同样的问题。只需安装此节点包并将这一行添加到您的 Gruntfile 中,require 应该会再次开始工作。
https://github.com/cloudchen/grunt-template-jasmine-requirejs
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')
}
}
},
回答by Justin Helmer
The solution that @user3741597is suggesting might work, but it is a bit of a backwards solution.
@user3741597建议的解决方案可能会奏效,但它有点向后解决方案。
"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-inand take a different approach.
"grunt-template-jasmine-requirejs" 是为 RequireJS 编写的,它是一个 AMD 加载器。另一方面,您使用的是 CommonJS 语法。如果你想继续使用“grunt-contrib-jasmine”,那么你需要找到一个CommonJS模板,或者使用Jasmine内置节点支持的信息并采取不同的方法。
This postmay help as well.
这篇文章也可能有所帮助。
回答by rogersillito
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:
从@justin-helmer的解决方案开始,有一个模板允许您使用带有 grunt-contrib-jasmine 的 require 调用(CommonJS 语法):
https://www.npmjs.com/package/grunt-template-jasmine-nml
https://www.npmjs.com/package/grunt-template-jasmine-nml
An example of grunt config using this:
使用此命令的 grunt 配置示例:
jasmine: {
options: {
template: require('grunt-template-jasmine-nml'),
helpers: 'spec/helpers/**/*.js',
specs: 'spec/**/*.spec.js'
}
}