簡體   English   中英

在使用Require的JavaScript上從Grunt運行Jasmine測試

[英]Running Jasmine Tests from Grunt on JavaScript that uses Require

我正在為lodash寫一些擴展。 可以從這里下載與此問題相關的代碼。 結構是代碼位於/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);
  });
});

此測試始終斷言為真。 我正在嘗試通過grunt執行此Jasmine測試。 當我通過Grunt執行this時,出現錯誤。 該錯誤使我感到困惑,因為在我的package.json文件中定義了grunt-jasmine-node模塊。 我還檢查了它在運行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.

這真令人沮喪。 我的代碼可以從這里下載。 我已經為此工作了2天。 如果今天沒有完成,則必須回到.NET。 有人可以幫我解決這個問題嗎? 我真的很想繼續朝這個方向前進。 我相信這只是一個很小的東西。

正如Andy指出的那樣,在package.json未定義grunt-jasmine-node

您可以使用命令npm-install --save grunt-jasmine-node定義並安裝它,以解決該錯誤。

這個問題可能與https://github.com/gruntjs/grunt/issues/232有關。

另外,您可能想分離開發依賴和常規依賴。

npm install --save-dev module在'devDependencies'配置中包含該模塊,並且

npm install --save modulepackage.json中的dependencies配置中包含該模塊。

我希望這能解決您的問題,我正在尋找500個賞金。

編輯

編輯:

在我看來,您正在將客戶端庫與服務器端混合在一起。

即,您包括這樣的供應商路徑:

文件: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 = _;

這又是服務器端代碼,該代碼已加載到瀏覽器中。 您應該為瀏覽器編寫此代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM