javascript - 使用 sinon 和 chai-as-promise 测试承诺拒绝时超时
问题描述
我有一个包装第三方的功能child-process-promise
,它本身包装spawn
在承诺中。
let spawn = require('child-process-promise').spawn;
run(cmd, args = []) {
return new Promise(async (resolve, reject) => {
let command = spawn(cmd, args);
let childProcess = command.childProcess;
let result = '';
childProcess.stdout.on('data', (data) => {
result += data.toString();
});
try {
const res = await command;
resolve(result);
} catch (err) {
if (err.code && err.code === 'ENOENT') {
reject(`Command "${cmd}" not found`);
} else {
reject('Exec err' + err);
}
}
});
}
测试解析非常简单,我设法将我的标准输出数据传递给结果,然后通过chai-as-promised
使用检测await expect(shellRun).to.eventually.become('hello world');
我们的问题是当我们尝试测试方法的 catch 部分时。
const ERROR = 'someError';
beforeEach(() => {
sandbox = sinon.createSandbox();
spawnEvent = new events.EventEmitter();
spawnEvent.stdout = new events.EventEmitter();
spawnStub = sandbox.stub();
spawnStub.returns({ childProcess: spawnEvent });
spawnStub.withArgs(ERRORED, ARGUMENTS).throws(ERROR));
shell = proxyquireStrict('../../lib/utils/spawnWrapper', {
'child-process-promise': {
spawn: spawnStub
}
}
);
});
afterEach(() => {
sandbox.restore();
});
describe('when a generic error occurs', () => {
it('should reject the promise', async () => {
const shellRun = run(ERRORED, ARGUMENTS);
await expect(shellRun).to.eventually.be.rejectedWith('Exec err' + ERROR);
});
});
我们设法childProcessPromiseSpawn
通过玩 ou 有条件地抛出错误spawnStub.withArgs
。但是遇到了超时:
(node:15425) UnhandledPromiseRejectionWarning: Error: someError
(node:15425) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 838)
1 failing
1) run method
when a generic error occurs
should reject the promise:
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
我们尝试spawnStub.withArgs(ERRORED, ARGUMENTS).rejects
了而不是throws
没有更多的成功。更改someError
为new Error('someError')
也不起作用。
我们还尝试在测试级别赶上
try {
await run(ERRORED, ARGUMENTS);
} catch (e) {
expect(e).to.equal('Exec err' + ERROR);
}
但是超时仍然发生。
解决方案
推荐阅读
- azure - Azure 数据流需要几分钟才能触发下一个管道
- javascript - 仅加载页面内的特定 DIV
- nrwl-nx - nrwl nx React 教程令人困惑
- python - 芹菜:通过send_task同时排队多个(100-1000)任务?
- javascript - 页面加载时抑制音频;用户脚本,时间问题
- r - 在绘图参数中定义颜色时,如何为顶点颜色属性制作图例?(右)
- mongodb - Docker:Mongodb容器错误执行“listdatabases”命令
- angular - 更改值访问器初始化后没有 NgControl 提供程序
- join - 在多个列上连接两个表,维护列之间的关系
- javascript - 如何防止用户超出 JSXGraph 中的某些坐标?