首页 > 解决方案 > 由于 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;
    });
});

标签: jestjscode-coveragets-jestjest-fetch-mock

解决方案


推荐阅读