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();
Edit: Noticed this in the shipped bootstrap.js
, which is basically the same (other than different naming)... So maybe this is normal?!
Setting up JasmineGive 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.
NODE AND BROWSER Jasmine is a behavior-driven development framework for testing JavaScript code. It does not depend on any other JavaScript frameworks. It does not require a DOM. And it has a clean, obvious syntax so that you can easily write tests.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With