0

I am writing some extensions to lodash. The code related to this question can be download from here. The structure is that code is located in /shared/modules/myExtensions.js. Currently, my code is very basic and looks like this:

'use strict';

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

module.exports = _;

My code will grow in complexity. For that reason, I want to setup unit tests from the start on this. Right now, my tests are located at /shared/tests/myExtensions.tests.js. That file looks like this:

'use strict';

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

This test always asserts to true. I'm trying to execute this Jasmine test via grunt. When I execute the this via Grunt, I get an error. The error confuses me because the grunt-jasmine-node module is defined in my package.json file. I've also checked that it got downloaded when I ran npm install. Either way, here is the error:

>> 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.

This is so frustrating. My code can be downloaded from here. I've been working on this for 2 days now. If I don't get it done today, I'll have to go back to .NET. Can someone please help me get this resolved? I really want to keep moving in this direction. I believe this is just something really small.

user70192
  • 13,786
  • 51
  • 160
  • 240

1 Answers1

3

grunt-jasmine-node is not defined in your package.json as Andy pointed out.

You can define and install it using the command npm-install --save grunt-jasmine-node that will fix that error.

This issue might be related https://github.com/gruntjs/grunt/issues/232.

Also you might want to seperate your dev dependencies and normal dependencies.

npm install --save-dev module includes the module in 'devDependencies' config, and

npm install --save module includes the module in dependencies config, in package.json.

I hope this will fix your problem, i am looking for that 500 bounty.

Edit

Edit:

Also it appears to me that you are mixing your client libraries with the server sides.

Namely you include vendor path like this:

File: 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",
         }

All your node_modules folder gets included inside the browser. Really what you should be doing is define your libraries in shared/libs and use that path for the vendor option.

You can use bower to automatically install them.

And finally your actual code,

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

module.exports = _;

This is again server side code, that gets loaded into the browser. You should write this for the browser.

eguneys
  • 6,028
  • 7
  • 31
  • 63
  • I would love to give you the bounty. However, it is STILL not working for me. I shared the code. Would you be open to modifying it and sharing it back? I'm really not sure why this isn't working. Thank you for your detailed response though. – user70192 Jul 19 '14 at 10:48
  • @user70192 Are you trying to write code that works on both server and browser, and test it on browser? – eguneys Jul 19 '14 at 12:27
  • I'm trying to write code that works on both server and browser. I'm trying to write automated tests that I can run via grunt. I would prefer NOT requiring a browser. If a browser is the only way, so be it. I just want to run the test via grunt. I'm so frustrated. Thank you for your help. – user70192 Jul 20 '14 at 14:37
  • I am not sure how to write code for both server and browser, but i know your current code above would not work on browser, that's why your tests fail, fix your code style so it works on both server and browser, maybe this will help you, and if it does, share your further problems or grant me that bounty: http://stackoverflow.com/questions/3225251/how-can-i-share-code-between-node-js-and-the-browser – eguneys Jul 20 '14 at 21:36
  • What about using browserify to allow server code to run in a browser? https://egghead.io/lessons/nodejs-introduction-to-browserify-part-1 – Eitan Peer Jul 23 '14 at 06:59