jasmine-node
Integration of Jasmine Spec framework with Node.js
Karma can not recognize 'require' statement in JSFileSpec.js file. Running karma.conf.js:
(function() {
describe("DummyEmitter creation", function(){
return it("creation", function(){
var DummyEmitter = require('Util.DummyEmitter');
var dummy = new DummyEmitter('someName');
return expect(dummy).toBeDefined();
});
});
})();
ReferenceError: require is not defined
Source: (StackOverflow)
I created a simple Webapp using express.js and want to test it with jasmine-node. Works fine so far but my problem is that I have to start the server manually every time before I can run my tests.
Could you help me on how to write a spec-helper that runs the server (with another port then my development one) just for the tests and then kills it afterwards?
Thanks, udo
Source: (StackOverflow)
I am using jasmine-node to run tests against my nodejs functions.
Being new to nodejs and mongodb, the first thing i ran into was testing some database calls, and I immediately got stuck, due to the asynchronous nature of nodejs.
What I want to do is:
1) Add an add
function to add new entries to a mongodb table
2) Receive a status string from that function to verify the action's status
The following is the code of my spec. In the beforeEach
call I initialise the database. As you can see in the implementation, it is only instantiated once, because of a condition asking if it already exists.
var mongo = require('../mongo.js');
describe('mongo', function() {
// generate a random number in order to test if the written item and the retrieved result match
var randomNumber = Math.random();
var item = {
'cities': {
'london': randomNumber
}
};
beforeEach(function() {
mongo.init();
waitsFor(function() {
return mongo.getCollection();
}, "should init the database", 10000);
});
it('should return "added" after adding an item to the database', function() {
var result;
waitsFor(function() {
result = mongo.add(item);
// the result value here is always undefined,
// due to the problem i'm having in my implementation
return result !== undefined;
}, "adding an item to the database", 10000);
runs(function() {
expect(result).toEqual('added');
});
});
});
Now, for every database query, I can define a callback function which is executed when the query has been run successfully. What I don't know how to achieve is delivering the result from the mongodb callback back the the spec.
This is the current implementation of the database functions:
var mongo = require('mongodb'),
Server = mongo.Server,
Db = mongo.Db;
var server = new Server('localhost', 27017, {auto_reconnect: true});
var db = new Db('exampleDb', server);
var collection = false;
// initialize database
var init = function() {
if (collection === false) {
db.open(dbOpenHandler);
}
};
var dbOpenHandler = function(err, db) {
db.collection('myCollection', dbCollectionHandler);
};
var dbCollectionHandler = function(err, coll) {
collection = coll;
};
/** returns the current db collection's status
* @return object db collection
*/
var getCollection = function() {
return collection !== false;
};
/** Add a new item to the database
* @param object item to be added
* @return string status code
*/
var add = function(item) {
var result = collection.insert( item, {safe: true}, function(err) {
// !! PROBLEM !!
// this return call returns the string back to the callee
// question: how would I return this as the add function's return value
return 'added';
});
};
// module's export functions
exports.init = init;
exports.getCollection = getCollection;
exports.add = add;
I'm also open for other approaches on how to test database calls in mongodb.
I've read a bunch of articles about this topic, but none of them covers my particular case.
SOLUTION
Finally, and with the help of JohnnyHK's answer, I managed to make it work with a callback.
Look at the following test case to understand what I did:
it('should create a new item', function() {
var response;
mongo.add(item, function( err, result) {
// set result to a local variable
response = result;
});
// wait for async call to be finished
waitsFor(function() {
return response !== undefined;
}, 'should return a status that is not undefined', 1000);
// run the assertion after response has been set
runs(function() {
expect(response).toEqual('added');
});
)}
Source: (StackOverflow)
How to run Jasmine tests on Node.js from command line?
I have installed jasmine-node via npm and wrote some tests.
I want to run tests inside spec directory and get results in terminal, is this possible ?
Source: (StackOverflow)
I have the following test code that is being ran by jasmine-node in a file called bob_test.spec.js
require('./bob');
describe("Bob", function() {
var bob = new Bob();
it("stating something", function() {
var result = bob.hey('Tom-ay-to, tom-aaaah-to.');
expect(result).toEqual('Whatever');
});
});
In order to make the test pass, I've written the following production code in a file called bob.js
"use strict";
var Bob = function() {
}
Bob.prototype.hey = function (text) {
return "Whatever";
}
module.exports = Bob;
When I run the test - using jasmine-node .
- I get the following
F
Failures:
1) Bob encountered a declaration exception
Message:
ReferenceError: Bob is not defined
Stacktrace:
ReferenceError: Bob is not defined
at null.<anonymous> (/Users/matt/Code/oss/deliberate-practice/exercism/javascript/bob/bob_test.spec.js:4:17)
at Object.<anonymous> (/Users/matt/Code/oss/deliberate-practice/exercism/javascript/bob/bob_test.spec.js:3:1)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
at require (module.js:380:17)
Finished in 0.02 seconds
1 test, 1 assertion, 1 failure, 0 skipped
Based on what I understand about Javascript, I feel like this should work. What does node.js do differently with constructor functions and module exports that prevents this from working I like think it should?
Source: (StackOverflow)
I expect this to say "1 test", but it says "0 tests". Any idea why? This is on OS X.
$ jasmine-node --verbose my.spec.js
undefined
Finished in 0.001 seconds
0 tests, 0 assertions, 0 failures, 0 skipped
$ cat my.spec.js
describe("bar", function() {
it("works", function() {
expect("foo").toEqual("foo");
});
});
$ jasmine-node --version
1.11.0
$ npm --version
1.3.5
$ node -v
v0.4.12
Even if I try to create a syntax error I get the same output:
$ cat my.spec.js
it(
$ jasmine-node --verbose --captureExceptions my.spec.js
undefined
Finished in 0.001 seconds
0 tests, 0 assertions, 0 failures, 0 skipped
But if I try to specify a file that doesn't exist, it complains:
$ jasmine-node no.spec.js
File: /tmp/no.spec.js is missing.
Source: (StackOverflow)
I set up grunt to run node.js jasmine tests. For some reason, with this config, the results always show double the tests.
Here is my config:
I'm using jasmine-node which plugs into grunt.
/spec/some-spec.js:
var myModule = require('../src/myModule.js');
describe('test', function(){
it('works', function(done){
setTimeout(function(){
expect(1).toBe(1);
done();
}, 100);
});
});
Gruntfile.js:
module.exports = function(grunt) {
grunt.initConfig({
jasmine_node: {
options: {
forceExit: true
},
all: ['spec/']
}
});
grunt.loadNpmTasks('grunt-jasmine-node');
grunt.registerTask('default', ['jasmine_node']);
};
This results in two tests running rather than one.
> grunt
Running "jasmine_node:all" (jasmine_node) task
..
Finished in 0.216 seconds
2 tests, 2 assertions, 0 failures, 0 skipped
Source: (StackOverflow)
onSaveEvent: function (event) {
if (this.model !== null) {
var that = this;
this.model.save(this.model.toJSON(), {
success: function (model) {
that.model = model;
that.model.attributes.isDirty = false;
},
error: function (model, xhr) {
that.model.attributes.isDirty = true;
}
});
}
}
}
how to unit test the model's save' sucess and error responses in Jasmine? sample code would help. thankx
Source: (StackOverflow)
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?!

Source: (StackOverflow)
I'm using jasmine-node to test my API, and it has worked great for my GET routes. Now, however, I need to test some POSTs and I'm not sure how to go about this without changing my database.
One thought I had was to reset whatever value I change at the end of each spec.
Is this reasonable or is there a better way to go about testing POST requests to my API?
Source: (StackOverflow)
Currently I have a set of node.js Javascript files along side a set of "regular" Javascript files that I will send down to a browser, using Angular as the client side framework. For unit testing I am using jasmine-node to test the server and am using jasmine via Karma for my client side files.
Is there a way I can include my jasmine-node files into Karma to have a single test runner?
Source: (StackOverflow)
I'm trying to get travis-ci to test my nodejs module with jasmine-node. When I run the tests from the commandline, they all pass, but for whatever reason, Travis always reports my build as failing. My .travis.yml looks like this:
language: node_js
node_js:
- 0.6
- 0.8
and my package.json looks like this:
"scripts": {
"test": "jasmine-node tests/*.spec.js"
}
I've tried adding a before_script to my travis.yml
language: node_js
node_js:
- 0.6
- 0.8
before_script:
- "sudo npm i -g jasmine-node"
Any ideas?
Source: (StackOverflow)
I have the following basic test for a web service:
var request = require('http'),
url = 'http://localhost:1337/';
describe('webservice', function() {
it('should respond to /ping', function(done) {
request.get(url + 'ping', function(res) {
expect(res.statusCode).toBe(200);
res.on('data', function(body) {
var json = JSON.parse(body);
expect(json.message).toBe("Hi, I'm alive");
expect(json.date).toBeDefined();
done();
});
});
});
});
which seems to work ok:
$ jasmine-node --verbose tests/ping/
webservice
should respond to /ping
Finished in 0.032 seconds
1 test, 3 assertions, 0 failures
But if I make one test fail (change the expected json message, for example) I get the following error:
$ jasmine-node --verbose --coffee tests/ping/
StreamWrap: Aborting due to unwrap failure at ../src/stream_wrap.cc:125
Aborted (core dumped)
Any idea what could I be doing wrong?
(more over, any mistake I have in my js, the test just silently fails, with no clue on waht happened, the js errors just dissapears)
update: the error appears only if the failing test is inside the request.get callback...
Source: (StackOverflow)
I'm trying to get the code below under test when occurred to me that I already included express at the top of this file. Can you some how monkey patch the express object after it's already loaded?
var express = require('express')
Helper = (function() {
var HelperObject = function(params) {
this.directories = params.directories;
};
HelperObject.prototype.addStaticPath = function(app) {
for(i = 0; i < this.directories.length; i++) {
var static = express.static('/public');
app.use(static);
}
};
return HelperObject;
})();
Source: (StackOverflow)
I'm using jasmine-node to test my Meteor application and I want to use the auto-test feature so I don't have to rerun the tests all the time by myself.
My meteor application folder structure is like this:
server
foo.coffee
tests
foo.spec.coffee
And with the spec file I want to test code which is located in foo.coffee. I start jasmine-node with this args:
jasmine-node ./ --autotest --coffee --test-dir tests
And now I would assume that the autotest feature will react on all changes in the root folder but it just reacts on changes in the test folder. And I can't start it in the root folder because I get an error in the .meteor files (and I don't want to have jasmine testing/including the meteor code anyway).
So I want to have jasmine rerun the tests even if I change code in the server folder. How can I achieve that?
Source: (StackOverflow)