ReferenceError: Can't find variable: require at

ReferenceError: Can't find variable: require at

本文关键字:find variable at require Can ReferenceError      更新时间:2023-09-26

我有一个关于在Grunt中使用jasmine的问题。总是出现错误

ReferenceError: Can't find variable: require at

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

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

我可以在没有require的情况下运行一个虚拟测试,但是当我在测试中包含一个require时,就像这样,我得到了require错误。

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

我也遇到了同样的问题。只需安装这个节点包并将这一行添加到您的Gruntfile中,require就可以重新开始工作了。

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

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

"grunt-template-jasmine- requires "是为AMD的一个加载程序requires编写的。另一方面,您正在使用CommonJS语法。如果你想继续使用"grunt-contrib-jasmine",那么你需要找到一个CommonJS模板,或者使用Jasmine内置节点支持的信息并采取不同的方法。

这篇文章可能也有帮助。

@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'
    }
}