在 Node.js 中使用 Jasmine 测试子进程发送
Testing child process.send with Jasmine in Node.js
我有一个 Node.js 应用程序,它有一个main-process.js
和一个child-process.js
。
main-process.js
如下所示:
var childProcess = require('child_process');
var job = childProcess.spawn('node', ["child-process.js"], {
detached = true,
stdio: ['ipc']
});
我的child-process.js
执行一些任务并通知父进程其状态,它使用:
exports.init = function() {
//some processing here
process.send({status: 'success or pending'});
}
现在我想使用 jasmine-node 对child-process.js
进行单元测试,但是当我从规范中调用init()
的方法时,jasmine-node
抛出了一个错误:
TypeError: Object #<process> has no method 'send'
有没有办法模拟process
变量?换句话说,如何对此方案进行单元测试?
有没有办法模拟过程变量?换句话说,如何对此方案进行单元测试?
没有必要在 child-process.js
中导出process.send()
功能。您可以将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
子进程.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)
规格.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()
}
})
})
)
输出
$ 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)
相关文章:
- 子进程未接收消息
- 如何通过子进程连接Java应用程序后在node.js中编写文件
- 节点.js子进程中的运行函数
- 如何将画布对象从子进程流式传输回 Node.js
- 如何在Firefox插件中向子进程发送消息,如Chrome原生消息
- 从生成的子进程中获取信息的最佳方法
- 在 Node.js 中使用 Jasmine 测试子进程发送
- 在快速服务器中生成子进程
- 如何在 node.js 中关闭子进程的 stdio 管道
- 如何使用并行子进程在大型数组上执行“工作”
- 使用 node.js (子进程) 运行 shell 脚本
- 如何从nodejs中的守护进程生成gpg子进程
- Cloud9 在生成子进程时抛出错误,是否有解决方法
- 如何在从 Meteor.method 返回之前等待子进程结果
- 将输入数据发送到节点.js中的子进程
- 完成时的子进程事件
- 如何在 Debian 上通过 node.js 中的引用变量停止子进程
- Node.js为什么不立即启动子进程
- 通过Node.js子进程关闭CS:GO专用服务器
- 如何重新启动Node.js子进程