简体   繁体   中英

Running Jasmine Tests from Grunt on JavaScript that uses Require

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM