Dinis Cruz Dinis Cruz - 2 months ago 7
Node.js Question

In Node, how to check that a particular object is an instance of ChildProcess

I'm trying to check (in an unit test) if a particular object is an ChildProcess, but I can't seem to get a reference to that class (which is here https://github.com/joyent/node/blob/7c0419730b237dbfa0ec4e6fb33a99ff01825a8f/lib/child_process.js )

what I would like do do is something like

selenium = require('selenium-standalone')
spawnOptions = { stdio: 'pipe' }
seleniumArgs = ['-Dwebdriver.chrome.driver=./node_modules/nodewebkit/nodewebkit/chromedriver']
@server = selenium(spawnOptions, seleniumArgs)


but I don't seem to figure out where to get the reference to ChildProcess

UPDATE: based on the answer provided, I'm now testing using:

it 'start()', (done)->
selenium.start ->


Unfortunately, child_process module doesn't export its ChildProcess class. You can easily access server's constructor, but you don't have anything to compare it with.

I can see two options for you.

Check that server's constructor name is ChildProcess

expect(server.constuctor).to.have.property('name', 'ChildProcess');

You may also add a check that server.constuctor is inherited from EventEmitter


Acquire ChildProcess class from spawn call

But it you want to be sure that server is a ChildProcess and not a mock, then you could acquire ChildProcess class from spawn call:

ChildProcess = require('child_process').spawn('echo').constructor; // use any command

the you'll be able to use it to verify your server object:


If you don't want to actually spawn anything, you could use any non-existing command. Just don't forget to specify an error handler to prevent child_process from throwing an error:

child = require('child_process').spawn('non-existing-command');
child.on('error', function() {}); // empty error handler
ChildProcess = child.constructor;