문제

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!

도움이 되었습니까?

해결책

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

다른 팁

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top