【问题标题】:ReferenceError: Can't find variable: require atReferenceError:找不到变量:需要在
【发布时间】:2014-05-03 20:04:05
【问题描述】:

我有一个关于在 Grunt 中使用 jasmine 的问题。我一直收到错误,

ReferenceError:找不到变量:需要在

每当我运行我的茉莉花测试时。这是我的 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);
  });
});

【问题讨论】:

    标签: javascript node.js gruntjs jasmine


    【解决方案1】:

    我遇到了同样的问题。只需安装这个节点包并将这一行添加到您的 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')
            }
          }
        },
    

    【讨论】:

    • 这实际上不起作用,因为 require js 的 require 语句与上面示例中给出的不同。
    【解决方案2】:

    @user3741597 建议的解决方案可能可行,但它有点倒退。

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

    This post 也可能有帮助。

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 2017-02-26
        • 2017-12-11
        • 2017-11-24
        • 2022-10-24
        • 1970-01-01
        • 1970-01-01
        • 2016-03-07
        • 2014-02-20
        • 2018-04-24
        相关资源
        最近更新 更多