Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jasmine-node - including helper

I am trying to test my Meteor application with jasmine-node. I've stubbed out some methods of Meteor framework in the helper (spec_helper.js):

  var Meteor = {
    startup: function (newStartupFunction) {
        Meteor.startup = newStartupFunction;
    },
    Collection: function (collectionName) {
        Meteor.instantiationCounts[collectionName] = Meteor.instantiationCounts[collectionName] ?
            Meteor.instantiationCounts[collectionName] + 1 : 1;
    },
    instantiationCounts: {}
  };

At this point I need to run the code in spec_helper.js (something equivalent of including a module in other languages). I've tried the following, but no success:

require(['spec_helper'], function (helper) {
    console.log(helper); // undefined
    describe('Testing', function () {
        it('should test Meteor', function () {
            // that's what I want to call from my stubs... 
            // ...it's obviously undefined
            Meteor.startup();
        });
    });
});

Any help would be greatly appreciated.

like image 979
alexs333 Avatar asked Jan 11 '23 04:01

alexs333


1 Answers

jasmine_node will autoload helpers (any file containing the word helpers) from within your spec directory.

NOTE: you can cheat and use helper instead since it's a substring of helpers...makes more sense if you split helpers out across multiple files...singular vs plural.

If you're executing your specs from specs/unit, then create a file named specs/unit/meteor-helper.js, and jasmine_node will automagically source it for you. It will load files with the extension .js if your specs are written in vanilla JavaScript. If you pass the --coffee switch on the command line or via your grunt task config (you may even be using gulp if you're ambitious), then it will load helpers with the extensions js|coffee|litcoffee.

You should export a hash from each helper file as follows:

specs/unit/meteor-helper.js

// file name must contain the word helper // x-helper is the convention I roll with module.exports = { key: 'value', Meteor: {} }

Then, jasmine_node will write each key to the global namespace.

This will allow you to simply type key or Meteor from your specs, or any system under test (typically code inside your lib folder that the specs are executing assertions against).

Additionally, jasmine_node will also allow you to suppress the loading of helpers via the --nohelpers switch (see code or README for more details).

This is the proper way to handle helpers for jasmine via node. You may come across some answers/examples that reference a jasmine.yml file; or maybe even spec_helper.js. But keep in mind that this is for ruby land and not node.

UPDATE: It appears jasmine-node will only source your file if it contains the word helpers. Naming each helper file x-helper.js|coffee|litcofee should do the trick. i.e. meteor-helper.coffee.

like image 92
A-Dubb Avatar answered Jan 20 '23 18:01

A-Dubb