【问题标题】:Getting an undefined value when trying to load Mocha through RequireJS尝试通过 RequireJS 加载 Mocha 时获取未定义的值
【发布时间】:2014-09-29 21:17:23
【问题描述】:

我开始编写测试用例,为此我使用require.js 加载mocha.js and chai.js 文件。为此,我编写了以下代码。

require(["tests/lib/mocha","tests/lib/chai"],function(mocha,chai){
  //loading all the test case files
  console.log(mocha);
  console.log(chai);
});

chai 工作正常,但mocha 得到undefined。我不知道有什么问题可以帮助我。

【问题讨论】:

  • 这段代码是否在浏览器中运行? (RequireJS 也可以在 Node 中使用,所以并不明显。)另外,请将您的 RequireJS 配置添加到您的问题中。

标签: javascript requirejs mocha.js testcase


【解决方案1】:

我遇到了同样的问题,使用 shim 导出解决了它。

require.config({
paths: {
    mocha: 'bower_components/mocha/mocha'
},
shim: {
    mocha: { exports: 'mocha' }
}
});

这很奇怪,因为 chai 不需要它来工作:-|。

【讨论】:

    【解决方案2】:

    你是在 Chrome 下运行的吗?这似乎是一个 Chrome 问题。 我遇到了同样的问题并切换到 Firefox 解决了它。 IE11也有效。 如果您想留在 Chrome 世界,Chrome Canary 也确实有效。

    【讨论】:

      猜你喜欢
      • 2019-10-21
      • 1970-01-01
      • 2012-03-07
      • 2019-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-18
      • 1970-01-01
      相关资源
      最近更新 更多