Tuesday, 15 June 2010

mocking - How to mock the Node.js child_process spawn function? -



mocking - How to mock the Node.js child_process spawn function? -

is there easy way mock node.js child_process spawn function?

i have code following, , test in unit test, without having rely on actual tool calls:

var output; var spawn = require('child_process').spawn; var command = spawn('foo', ['get']); command.stdout.on('data', function (data) { output = data; }); command.stdout.on('end', function () { if (output) { callback(null, true); } else { callback(null, false); } });

is there (proven , maintained) library allows me mock spawn phone call , lets me specify output of mocked call?

i don't want rely on tool or os maintain tests simple , isolated. want able run tests without having set complex test fixtures, mean lot of work (including changing scheme configuration).

is there easy way this?

i've found mock-spawn library, pretty much want. allows mock spawn phone call , provide expected results calling test.

an example:

var mockspawn = require('mock-spawn'); var myspawn = mockspawn(); require('child_process').spawn = myspawn; myspawn.setdefault(myspawn.simple(1 /* exit code */, 'hello world' /* stdout */));

more advanced examples can found on project page.

node.js mocking spawn

No comments:

Post a Comment