Javascript 在Node.js中使用Jasmine测试child process.send
我有一个Node.js应用程序,它有一个Javascript 在Node.js中使用Jasmine测试child process.send,javascript,node.js,unit-testing,testing,jasmine-node,Javascript,Node.js,Unit Testing,Testing,Jasmine Node,我有一个Node.js应用程序,它有一个主进程.js和一个子进程.js main process.js如下所示: var childProcess = require('child_process'); var job = childProcess.spawn('node', ["child-process.js"], { detached = true, stdio: ['ipc'] }); Mychild process.js执行一些任务,并使用以下命令通知父进程其状态: expo
主进程.js
和一个子进程.js
main process.js
如下所示:
var childProcess = require('child_process');
var job = childProcess.spawn('node', ["child-process.js"], {
detached = true,
stdio: ['ipc']
});
Mychild process.js
执行一些任务,并使用以下命令通知父进程其状态:
exports.init = function() {
//some processing here
process.send({status: 'success or pending'});
}
现在我想使用单元测试child process.js
,但是当我从规范中调用方法init()
时,jasmine node
抛出一个错误:
TypeError: Object #<process> has no method 'send'
TypeError:对象#没有方法“send”
有没有办法模拟过程
变量?换句话说,我如何对这个场景进行单元测试
有没有办法模拟流程变量?换句话说,我如何对这个场景进行单元测试
无需导出子进程.js中的进程.send()
功能。
您可以将process.send()
放在正文中的任何位置,以便与main process.js进行通信
我已使用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
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)
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()
}
})
})
)
子进程.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
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)
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()
}
})
})
)
spec.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
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)
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()
}
})
})
)
输出
$ 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)