javascript - 在使用 Require 的 JavaScript 上从 Grunt 运行 Jasmine 测试

标签 javascript node.js gruntjs jasmine

我正在为 lodash 编写一些扩展。这个问题相关的代码可以从here下载。其结构是代码位于/shared/modules/myExtensions.js 中。目前,我的代码非常基本,如下所示:

'use strict';

var _ = require('lodash');
_.mixin({
  'myFunction' : function(s) {
    return 'Hello ' + s; 
  }
});

module.exports = _;

我的代码将会变得越来越复杂。因此,我想从一开始就设置单元测试。现在,我的测试位于/shared/tests/myExtensions.tests.js。该文件如下所示:

'use strict';

describe('myModule', function() {
  it('should work', function() {
    expect(true).toBe(true);
  });
});

此测试始终断言为 true。我正在尝试通过 grunt 执行这个 Jasmine 测试。当我通过 Grunt 执行 this 时,出现错误。该错误让我感到困惑,因为 grunt-jasmine-node 模块是在我的 package.json 文件中定义的。我还检查了运行 npm install 时它是否已下载。不管怎样,错误如下:

>> Local Npm module "grunt-jasmine-node" not found. Is it installed?

Running "jasmine:testShared" (jasmine) task
Testing jasmine specs via PhantomJS

>> Error: notloaded: Module name "../" has not been loaded yet for context: _. Use require([])
>> http://requirejs.org/docs/errors.html#notloaded at
>> ..\..\C:\Tests\jasmine\_SpecRunner.html:21
>> ..\..\C:\Tests\jasmine\.grunt\grunt-contrib-jasmine\require.js:12 v
>> ..\..\C:\Tests\jasmine\.grunt\grunt-contrib-jasmine\require.js:26 h
>> ..\..\C:\Tests\jasmine\.grunt\grunt-contrib-jasmine\require.js:31
>> ..\..\C:\Tests\jasmine\node_modules\glob\examples\g.js:1
>> Error: notloaded: Module name "../" has not been loaded yet for context: _. Use require([])
>> http://requirejs.org/docs/errors.html#notloaded at
>> ..\..\C:\Tests\jasmine\_SpecRunner.html:21
>> ..\..\C:\Tests\jasmine\.grunt\grunt-contrib-jasmine\require.js:12 v
>> ..\..\C:\Tests\jasmine\.grunt\grunt-contrib-jasmine\require.js:26 h
>> ..\..\C:\Tests\jasmine\.grunt\grunt-contrib-jasmine\require.js:31
>> ..\..\C:\Tests\jasmine\node_modules\glob\examples\usr-local.js:1
>> ReferenceError: Can't find variable: module at
>> ..\..\C:\Tests\jasmine\node_modules\glob\glob.js:37
>> Error caught from PhantomJS. More info can be found by opening the Spec Runner in a browser.
Warning: SyntaxError: Parse error Use --force to continue.

Aborted due to warnings.

这太令人沮丧了。我的代码可以从here下载。我已经为此工作了两天了。如果我今天没有完成它,我将不得不回到.NET。有人可以帮我解决这个问题吗?我真的很想朝着这个方向继续前进。我相信这只是很小的事情。

最佳答案

正如 Andy 指出的那样,

grunt-jasmine-node 未在您的 package.json 中定义。

您可以使用命令npm-install --save grunt-jasmine-node定义并安装它,这将修复该错误。

此问题可能与 https://github.com/gruntjs/grunt/issues/232 相关.

此外,您可能希望将开发依赖项和普通依赖项分开。

npm install --save-dev module 在“devDependency”配置中包含该模块,并且

npm install --save modulepackage.jsondependencies 配置中包含该模块。

我希望这能解决您的问题,我正在寻找 500 美元的赏金。

编辑

编辑:

在我看来,您正在将客户端库与服务器端混合在一起。

也就是说,您包括这样的 vendor 路径:

文件:tasks/options/jasmine.js

options: {
         specs: "shared/tests/unit/**.tests.js",
         // server libs
         vendor: "node_modules/**/*.js",
         // should be browser libs
         // vendor: "shared/libs/lodash/dist/lodash.js",
         }

您的所有 node_modules 文件夹都会包含在浏览器中。 实际上你应该做的是在 shared/libs 中定义你的库 并将该路径用于 vendor 选项。

您可以使用 Bower 自动安装它们。

最后是你的实际代码,

var _ = require('lodash');
_.mixin({
  'myFunction' : function(s) {
    return 'Hello ' + s;
  }
});

module.exports = _;

这又是服务器端代码,被加载到浏览器中。 您应该为浏览器编写此代码。

关于javascript - 在使用 Require 的 JavaScript 上从 Grunt 运行 Jasmine 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24779226/

相关文章:

php - 使用 Ajax 和 3 个连续的可靠下拉列表

javascript - 在 CSS 和 Vue 中将非事件缩略图显示为灰色

javascript - 设置基本的 Node.js 服务器

javascript - 当正文包含特殊字符时, Node 请求失败

javascript - 拆分 Gruntfile

javascript - Grunt 登录已注册的任务?

javascript - 在没有 Handlebars 的情况下使用 Grunt 和 Assemble(使用 Consolidatejs/Swig)?

javascript - 我是否将 Javascript 用于 HTML <select> 需要影响两个变量?

javascript - 如何设置在 HTML5 <canvas> 中绘制文本的最大高度?

node.js - 如何在 mongoose/mongodb 查询子文档中使用 mapreduce?