我如何禁用开玩笑的未置换检测
我正在编写测试并模拟故障API响应。在这种情况下,充当该API的客户的库会产生未被拒绝的库。
这是图书馆中的100%错误,我们正在贬低该库,但是远离它的库将需要更多的几周,我今天需要测试通过。
不幸的是,开玩笑没有提供禁用此行为的功能。 Jest-Circus添加了自己的处理程序,并且未能通过测试。这发生在 jest-circus/src/globalrorhandlers.ts 。
我已经尝试在该文件中评论uck fused
的主体,但确实确实“解决”了我的问题,这证实了这些处理程序是我想要的。
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
const uncaught = error => {
// commenting-out this call to dispatchSync "fixes" the issue
// (0, _state.dispatchSync)({
// error,
// name: 'error'
// });
};
我的下一个想法是做一个process.removealllisteners('Unhandledseptiondements');
在我的测试中(粗略地说),但即使使用运行
-runinband 。
经过一番调试后,我发现:
process.pid
在我的测试中,parentprocess.pid
jest-circus中的是相等的,但是process> process.listenercount('UnhandhandLeDledection ')
和parentprocess.listenercount('Unhandledseptiondeys')
不是(分别为零和一个),并且process.mainmodule
是我的测试中的
undefined ,但parentprocess.mainmodule
在jest-circus中定义/BIN/JEST.JS )。
那完全使我感到困惑。它们似乎是不同的过程,但共享相同的pid
。
要进一步调试,我做了a
// in my test
beforeAll(() => {
process.emitWarning('emitted warning process from my test. count: ' + process.listenerCount('unhandledRejection'));
});
// in globalErrorHandlers.js
parentProcess.on('warning', warning => {
console.log('parentProcess warning', parentProcess.listenerCount('unhandledRejection'), ' --- ', warning);
});
,此输出parentprocess警告1 ---警告:测试中发出警告过程。计数:0
到控制台,我想这表明它们是相同的过程(这很有意义),但是lincerercount
有所不同!
附带说明,Jest-Circus'InjectGlobalErrorhandlers
始终在我的测试代码,甚至全局空间代码之前运行。 RETAREGLOBALERRORHANDLERS
在之后始终运行。
I'm writing a test and simulates a failure API response. The library that acts as a client for that API generates an uncaught rejection in this case.
This is 100% a bug in the library and we're on the process of deprecating that library, but migrating away from it will take some more weeks and I need my test passing today.
Unfortunately, Jest does not offer a functionality to disable this behavior. jest-circus adds its own handlers and fails the test. This happens in jest-circus/src/globalErrorHandlers.ts.
I've tried commenting-out the body of uncaught
in that file and that does indeed "fix" my problem, which confirms these handlers are what I'm looking for.
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
const uncaught = error => {
// commenting-out this call to dispatchSync "fixes" the issue
// (0, _state.dispatchSync)({
// error,
// name: 'error'
// });
};
My next thought was to do a process.removeAllListeners('unhandledRejection');
in my test (roughly speaking), but that doesn't work, even when running with --runInBand
.
After some debugging, I found out that:
process.pid
in my test andparentProcess.pid
in jest-circus are equal, butprocess.listenerCount('unhandledRejection')
andparentProcess.listenerCount('unhandledRejection')
are not (zero and one, respectively), andprocess.mainModule
isundefined
in my test butparentProcess.mainModule
is defined in jest-circus (filename: .../jest/bin/jest.js
).
That confused me completely. They seem to be different processes, yet share the same pid
.
To debug further, I did a
// in my test
beforeAll(() => {
process.emitWarning('emitted warning process from my test. count: ' + process.listenerCount('unhandledRejection'));
});
// in globalErrorHandlers.js
parentProcess.on('warning', warning => {
console.log('parentProcess warning', parentProcess.listenerCount('unhandledRejection'), ' --- ', warning);
});
And this outputs parentProcess warning 1 --- Warning: emitted warning process from my test. count: 0
to the console, which I guess would indicate they are the same process (which makes sense), yet somehow the listenerCount
differs!
As a side note, jest-circus' injectGlobalErrorHandlers
always runs before my test code, even global-space code. restoreGlobalErrorHandlers
always runs after.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
似乎自定义的嘲笑环境可以解决问题:
取自 >在GitHub问题上。
It seems a custom Jest environment will do the trick:
Taken from this comment on a GitHub issue.