ReferenceError:无法找到变量:require at

时间:2014-05-03 20:04:06

标签: javascript node.js gruntjs jasmine

我有一个关于将茉莉花与Grunt一起使用的问题。我一直收到错误,

ReferenceError:找不到变量:require

每当我进行茉莉花测试时。这是我的Gruntfile.js的茉莉花条目:

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

我可以在没有要求的情况下运行虚拟测试,但是当我在测试中包含require时,我会收到require错误。

var testD = require('../src/events_to_actions');

describe("events_to_actions", function() {
  it("is dummy test", function() {
    expect(true).toEqual(true);
  });
});

3 个答案:

答案 0 :(得分:7)

我遇到了同样的问题。只需安装此节点包并将此行添加到您的Gruntfile,并且需要再次开始工作。

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')
        }
      }
    },

答案 1 :(得分:4)

@ user3741597 建议的解决方案可能有效,但这是一个向后的解决方案。

“grunt-template-jasmine-requirejs”是为RequireJS编写的,它是一个AMD加载程序。另一方面,您使用的是CommonJS语法。如果你想继续使用“grunt-contrib-jasmine”,那么你需要找到一个CommonJS模板,或者使用Jasmine has node support built-in的信息并采取不同的方法。

This post也可能会有所帮助。

答案 2 :(得分:0)

@ justin-helmer 引入解决方案,有一个模板允许您对grunt-contrib-jasmine使用require调用(CommonJS语法):

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

使用此命令的grunt配置示例:

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