在Node.js中使用Jasmine测试子进程.send

Sri*_*hsa 10 javascript testing unit-testing node.js jasmine-node

我有一个具有a main-process.js和a 的Node.js应用程序child-process.js.

main-process.js如下所示:

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

child-process.js做了一些任务并通知父进程有关其状态,它使用:

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

现在我想child-process.js使用jasmine-node进行单元测试,但是当我init()从规范中调用方法时,jasmine-node会抛出一个错误:

TypeError: Object #<process> has no method 'send'
Run Code Online (Sandbox Code Playgroud)

有没有办法模拟process变量?换句话说,我该如何对这种情况进行单元测试?

jon*_*sam 1

有什么方法可以模拟流程变量吗?换句话说,我如何对这个场景进行单元测试?

没有必要process.send()导出child-process.js. 你可以把它放在process.send()身体的任何地方进行交流main-process.js

我已经使用 Jasmine 成功运行了下面的代码:

主进程.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
Run Code Online (Sandbox Code Playgroud)

子进程.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)
Run Code Online (Sandbox Code Playgroud)

规范.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()
            }
        })
    })
)
Run Code Online (Sandbox Code Playgroud)

输出

$ npm test

> so-jasmine-test@1.0.0 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)
Run Code Online (Sandbox Code Playgroud)