Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why is jasmine-node not picking up my helper script?

This question is likely based in my lack of previous experience with node.js, but I was hoping jasmine-node would just let me run my jasmine specs from the command line.

TestHelper.js:

var helper_func = function() {
    console.log("IN HELPER FUNC");
};

my_test.spec.js:

describe ('Example Test', function() {
  it ('should use the helper function', function() {
    helper_func();
    expect(true).toBe(true);
  }); 
});

Those are the only two files in the directory. Then, when I do:

jasmine-node .

I get

ReferenceError: helper_func is not defined

I'm sure the answer to this is easy, but I didn't find any super-simple intros, or anything obvious on github. Any advice or help would be greatly appreciated!

Thanks!

like image 855
Hoopes Avatar asked Apr 10 '12 01:04

Hoopes


People also ask

How do you set up a jasmine test?

Setting up Jasmine Give your code access to Jasmine, downloading it manually or with a package manager. Initialize Jasmine. Create a spec (test) file. Make the source code available to your spec file.

Can jasmine be integrated with node js?

We can install Jasmine for node. js by running npm install jasmine-node -g . We provide the -g option to install it globally, so we're able to run jasmine-node from the command line. jasmine-node spec looks for specs in the spec directory and finishes successfully, because none of our 0 specs failed.

What are helpers node?

The node helper ( node_helper. js ) is a Node. js script that is able to do some backend task to support your module. For every module type, only one node helper instance will be created. For example: if your MagicMirror uses two calendar modules, there will be only one calendar node helper instantiated.


1 Answers

In node, everything is namespaced to it's js file. To make the function callable by other files, change TestHelper.js to look like this:

var helper_func = function() {
    console.log("IN HELPER FUNC");
};
// exports is the "magic" variable that other files can read
exports.helper_func = helper_func;

And then change your my_test.spec.js to look like this:

// include the helpers and get a reference to it's exports variable
var helpers = require('./TestHelpers');

describe ('Example Test', function() {
  it ('should use the helper function', function() {
    helpers.helper_func(); // note the change here too
    expect(true).toBe(true);
  }); 
});

and, lastly, I believe jasmine-node . will run every file in the directory sequentially - but you don't need to run the helpers. Instead you could move them to a different directory (and change the ./ in the require() to the correct path), or you could just run jasmine-node *.spec.js.

like image 186
Nathan Friedly Avatar answered Sep 28 '22 06:09

Nathan Friedly