jestjs - 由于 index.d.ts,Jest 无法覆盖构造函数中的行
问题描述
在测试用例代码覆盖率方面需要帮助。
export class TestChatDrawer extends TestContainerBaseElement {
private chatDrawerTargetElementId = 'chat-drawer';
private chatDrawerPageId = 'Test_com_checkout';
constructor() {
super();
this.init();
}
private init() {
if (Test.ContactDrawer) {
const { sc }: Metrics = window.Test.Metrics;
Test.ContactDrawer.init({
targetElementId: this.chatDrawerTargetElementId,
country: sc.country,
language: sc.language,
segment: sc.segment,
pageId: this.chatDrawerPageId,
});
}
}
render() {
return template.call(this);
}
}
文件名:index.d.ts
declare namespace Test.ContactDrawer
{
function init(param:any);
}
测试用例
it('应该处理调度事件和redirectToCreateAccount方法', async () => { const pppp = Test.ContactDrawer.init({ targetElementId: 'chat-drawer', country: 'India', language: 'English', segment: '测试', pageId: '测试抽屉', });
Test.ContactDrawer.init = jest.fn().mockImplementationOnce(() => {
Test.ContactDrawer.init(() => {
pppp;
});
});
_sut = await createSut(new TestChatDrawer());
expect(_sut.querySelectorAll('#dds__drawer').length).toEqual(0);
});
异常抛出的行
const pppp = Test.ContactDrawer.init({ targetElementId: 'chat-drawer', country: 'India', 语言: 'English', 段: 'Test', pageId: 'Testing-drawer', });
Test.ContactDrawer.init = jest.fn().mockImplementationOnce(() => {
Test.ContactDrawer.init(() => {
pppp;
});
});
解决方案
推荐阅读
- java - Selenium Java-下拉选择,其中样式-显示为无
- javascript - 将字符串参数传递给 onclick() 函数并以模式显示
- reactjs - 在 React 的状态字典中设置字典
- php - PHP ldap_search 给出错误“错误的搜索过滤器”(-7)
- android - Firebase ref.setValue(ServerValue.TIMESTAMP) 设置一个巨大的值(例如 1570654658313)
- ldap - 使用 ldapsearch 进行 G Suite LDAP 搜索
- reactjs - 如何在获取请求中获取响应的标头?
- graphql - 在页面末尾使用命名导出时,Gatsy 页面查询无法正常工作。为什么?
- json - 如何使用 Newtonsoft.Json.JsonConvert.SerializeObject 序列化类以将空列表更改为空变量
- java - 某些请求没有检测到 RestController 的原因是什么