What's the correct way to use Jasmine from Node?
Asked Answered
C

2

9

After much hacking, I've managed to get a simple Jasmine test running via Node.

However, there is some weird stuff I do not understand... The jasmine files export functions that appear to need a reference to themselves passed back in to work (this goes for both Jasmine and the ConsoleReporter).

I'm certain this isn't the correct way to do this (though I'm happy that I finally made some tests run :)), so can someone explain the better way to do this?

(Note: I'm not interested in pulling in more third party code I don't understand like node-jasmine; I want to understand what I had for now; not add more!)

// Include stuff
jasmine = require('../../../Apps/Jasmine/jasmine-standalone-2.0.0/lib/jasmine-2.0.0/jasmine.js');
jasmineConsole = require('../../../Apps/Jasmine/jasmine-standalone-2.0.0/lib/jasmine-2.0.0/console.js')


// WHAT THE? I DON'T EVEN KNOW WHAT THIS MEANS
jasmine = jasmine.core(jasmine);
jasmineConsole.console(jasmineConsole, jasmine)


// Set up the console logger
jasmine.getEnv().addReporter(new jasmine.ConsoleReporter({ print: console.log }));


// Create some global functions to avoid putting jasmine.getEnv() everywhere
describe = jasmine.getEnv().describe;
it = jasmine.getEnv().it;
expect = jasmine.getEnv().expect;


// Dummy tests

describe("A suite", function() {
    it("contains spec with an expectation", function() {
        expect(true).toBe(true);
    });
    it("contains spec with a failing expectation", function() {
        expect(true).toBe(false);
    });
});


// Kick off execution

jasmine.getEnv().execute();

Jasmine "working"

Edit: Noticed this in the shipped bootstrap.js, which is basically the same (other than different naming)... So maybe this is normal?!

It's not just me doing bonkers stuff!

Concessive answered 2/3, 2014 at 12:20 Comment(0)
O
6

Pivatol recently added better node.js support to Jasmine in 2.0 and plans to release an official NPM package. For now you can use it from node by following the implementation used in their own node test suite.

Here is a brief explanation into what is happening under the covers in the code you wrote:

jasmine = jasmine.core(jasmine); When you initially require('jasmine') you are getting back a single function, getJasmineRequiredObj(); By calling jasmine.core(jasmine), you are tell jasmine to return it's behavioral methods using node exports statements instead of adding them onto the window.jasmineRequire object.

https://github.com/pivotal/jasmine/blob/master/src/core/requireCore.js

function getJasmineRequireObj() {
  if (typeof module !== 'undefined' && module.exports) {
    return exports;  
  } else {
    window.jasmineRequire = window.jasmineRequire || {};
    return window.jasmineRequire;
  }
}

// jRequire is window.jasmineRequire in a browser or exports in node.
getJasmineRequireObj().core = function(jRequire) {
  var j$ = {};

  jRequire.base(j$);
  j$.util = jRequire.util();
  j$.Any = jRequire.Any();
  ...
  return j$; // here is our jasmine object with all the functions we want.
};

jasmineConsole.console(jasmineConsole, jasmine) Jasmine initializes it's core functions separately from it's reporters. This statement is essentially the same thing as jasmine.core(jasmine) only for the console reporter.

https://github.com/pivotal/jasmine/blob/master/src/console/requireConsole.js

Orthogenic answered 2/3, 2014 at 13:53 Comment(5)
So are you saying what I've done is the correct way to use this outside of a browser? It looks rather weird like I bodged something!Concessive
I looked at node-jasmine, but it seems to have a load of code in it, and I didn't understand why (seems like Jasmine should support running in node without a lot of work); and I wasn't sure if it was maintained by the Jasmine authors. If this doesn't look bonkers, I'll stick with it for now!Concessive
Looks like the node support is new to 2.0. I expect that all that extra code was needed in 1.3.x github.com/pivotal/jasmine/blob/v2.0.0/release_notes/…Orthogenic
Aha! That page links to github.com/pivotal/jasmine/blob/master/spec/node_suite.js which looks like exactly what I couldn't find - a node version of boot.js! I'll switch over to that, should be cleaner! Thanks!Concessive
(possibly worth throwing a link to node_suite.js into your answer to make it easier to find for anyone else that stumbles across this!)Concessive
T
3

There is also jasmine-node (that still uses jasmine 1.3 and has a beta version with jasmine 2.0 - February 2015) and jasmine-npm (from the jasmine maintainers themselves, with the latest version).

Both of them are easy to use from the command line, with no code required (except the tests, of course!).

Theodore answered 9/2, 2015 at 9:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.