gpt4 book ai didi

javascript - 在 Node.js 中使用 Jasmine 测试子 process.send

转载 作者:数据小太阳 更新时间:2023-10-29 03:57:50 26 4
gpt4 key购买 nike

我有一个 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-nodechild-process.js 进行单元测试,但是当我从规范中调用方法 init() 时,jasmine-node 抛出错误:

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

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

最佳答案

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

不必在 child-process.js 中导出 process.send() 功能。您可以将 process.send() 放在正文的任何​​位置,以便与 main-process.js

进行通信

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

ma​​in-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

子进程.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()
}
})
})
)

输出

$ 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)

关于javascript - 在 Node.js 中使用 Jasmine 测试子 process.send,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28110241/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com