nodeunit

Use Jasmine to stub JS callbacks based on argument values

拟墨画扇 提交于 2019-12-04 17:11:29
I've got a JS method in my node.js app that I want to unit test. It makes several calls to a service method, each time passing that service a callback; the callback accumulates the results. How can I use Jasmine to stub out the service method so that each time the stub is called, it calls the callback with a response determined by the arguments? This is (like) the method I'm testing: function methodUnderTest() { var result = []; var f = function(response) {result.push(response)}; service_method(arg1, arg2, f); service_method(other1, other2, f); // Do something with the results... } I want to

Overriding functions in other modules in node.js

时间秒杀一切 提交于 2019-12-04 12:44:16
I'm trying to stub a function with nodeunit in a Node.js app. Here's a simplified version of what I'm trying to do: In lib/file.js : var request = require('request'); var myFunc = function(input, callback){ request(input, function(err, body){ callback(body); }); }; In test/test.file.js : var file = require('../lib/file'); exports['test myFunc'] = function (test) { request = function(options, callback){ callback('testbody'); }; file.myFunc('something', function(err, body){ test.equal(body, 'testbody'); test.done(); }); }; It seems like I'm not overriding request properly, because when I try to

How to add custom assertions in Nodeunit

我的梦境 提交于 2019-12-04 09:04:28
Is there a way of adding custom assertions to the NodeUnit test object that gets passed to each test? I'd like to do something like: var Test = require('nodeunit').Test; Test.prototype.customAssertion = function(obj) { test.same(obj.foo, 'bar'); test.same(obj.bar, 'baz'); } exports.test = function(test) { test.customAssertion(obj); test.done(); } var assert = require('nodeunit').assert; var testCase = require('nodeunit').testCase; assert.isVowel = function(letter, message) { var vowels = [ 'a', 'e', 'i', 'o', 'u' ]; if (vowels.indexOf(letter) == -1) { assert.fail(letter, vowels.toString(),

how to debug nodeunit using node-inspector

ぃ、小莉子 提交于 2019-12-03 02:41:25
I can do: I can test node.js modules using nodeunit . I can debug my node.js express site using node inspector . But how to debug nodeunit test using node inspector? I tried, but not working: nodeunit --debug myNodeUnitModule_test.js It's not working. I tried to install nodebug . And used it like this: nodebug /usr/local/bin/nodeunit myNodeunit_test.js But it's not working neither on ubuntu ( No such file or directory ) nor on mac ( env: node\r: No such file or directory ) Almost works node --debug /usr/local/bin/nodeunit ./routes/edit/bodyTelInfoArraysToObject_test.js where /usr/local/bin