Testing child process.send with Jasmine in Node.js
Asked Answered
H

1

12

I have a Node.js application that has a main-process.js and a child-process.js.

The main-process.js looks like this:

var childProcess = require('child_process');
var job = childProcess.spawn('node', ["child-process.js"], {
  detached = true,
  stdio: ['ipc']
});

My child-process.js does some task and to notify the parent process about its status, it uses:

exports.init = function() {
   //some processing here
   process.send({status: 'success or pending'});
}

Now I want to unit test child-process.js using jasmine-node, but when I call the method init() from the spec, jasmine-node is throwing an error:

TypeError: Object #<process> has no method 'send'

Is there any way to mock the process variable? In other words, how do I unit test this scenario?

Hunnicutt answered 23/1, 2015 at 12:43 Comment(0)
S
1

Is there any way to mock the process variable? In other words, how do I unit test this scenario?

It's not necessary to export the process.send() functionality in child-process.js. You can put process.send() anywhere in the body to communicate back with main-process.js

I've successfully run the code below using Jasmine:

main-process.js

var childProcess = require('child_process');

// set an infinite loop that keeps main-process running endlessly
setInterval(() => null, 5000)

// spawn child process
var job = childProcess.spawn('node', ["child-process.js"], {
  detached: true,
  stdio: ['ipc']
});

// listen to the events returned by child process
job.on('message', (code, signal) =>
  console.log('[MAIN] received the ff from child process:', {code, signal}, 'at', new Date())
)

// export the job object, which is an event emitter that can be tested by Jasmine pkg
module.exports = job

child-process.js

const message = {
    message: `I'm working on it...`,
    status: 'success or pending'
}

// send a message to the parent every half second
setInterval(() => process.send(message), 500)

spec.js

const main = require('../main-process')

describe('main process', () =>
    it('should receive messages from spawned child process', (done) => {
        let eventCount = 0
        main.on('message', (code, signal) => {
            console.log('[JASMINE] received the event')
            eventCount++
            if (eventCount >= 5) {
                done()
            }
        })
    })
)

output

$ npm test

> [email protected] test C:\Users\jonathanlopez\nodejs\so-jasmine-test
> jasmine

Randomized with seed 29172
Started
[MAIN] received the ff from child process: { code:
   { message: 'I\'m working on it...',
     status: 'success or pending' },
  signal: undefined } at 2018-10-17T08:50:51.559Z
[JASMINE] received the event
[MAIN] received the ff from child process: { code:
   { message: 'I\'m working on it...',
     status: 'success or pending' },
  signal: undefined } at 2018-10-17T08:50:52.060Z
[JASMINE] received the event
[MAIN] received the ff from child process: { code:
   { message: 'I\'m working on it...',
     status: 'success or pending' },
  signal: undefined } at 2018-10-17T08:50:52.562Z
[JASMINE] received the event
[MAIN] received the ff from child process: { code:
   { message: 'I\'m working on it...',
     status: 'success or pending' },
  signal: undefined } at 2018-10-17T08:50:53.064Z
[JASMINE] received the event
[MAIN] received the ff from child process: { code:
   { message: 'I\'m working on it...',
     status: 'success or pending' },
  signal: undefined } at 2018-10-17T08:50:53.565Z
[JASMINE] received the event
.


1 spec, 0 failures
Finished in 2.736 seconds
Randomized with seed 29172 (jasmine --random=true --seed=29172)
Santiago answered 17/10, 2018 at 9:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.