Replies: 2 comments 4 replies
-
|
我也是用 mocha, 只在全局初始化一次 mockApp。不过不能并发测试 |
Beta Was this translation helpful? Give feedback.
2 replies
-
|
@czy88840616 挺哥,上次聊到可以通过 mock 分组来支持,可否进一步描述这个方案,或者发一下相关的开发文档/代码,我可以尝试 pr 实现 |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
The feature, motivation(功能、动机)
当前提供的 restoreAllMocks() 方法调用的是 MockService#restore(),后者除了会重置掉所有的 mock,还会清除原先配置过的 cache,也就是各种开发期 mock 和单元测试里调用的 mockClassProperty、mockContext etc.
给标准应用单元测试时,有一种效率比较高的单元测试执行方式:
这种执行方式,只需要调用一次 createApp。在实际案例中,由于 jest 默认是并发执行测试,beforeAll 只在当前测试文件生效,如果测试文件较多,仍然会出现需要多次 createApp 的情况,作为对比,我将运行框架替换成 mocha,原先需要 10 分钟跑完的单测,现在 5 分钟就可以
但是这种执行方式在第三步没办法直接用 restoreAllMocks(),因为这个方法会连同开发期 mock 一并清除,并且是连 cache 一起干掉,所以提议给 MockService 增加一个仅重置 mock 的方法,例如 MockService#restoreOnly(),保留 simulartorList 即可
Alternatives(替代方案)
也可以调整现有的 MockService#restore() 方法,另外增加一个 restoreAndDestroy(),后者再去清除 simulatorList 以及处理 service destroy
Additional context(其他上下文)
No response
Beta Was this translation helpful? Give feedback.
All reactions