【问题标题】:Jasmine.createSpy equivalent in jest开玩笑的 Jasmine.createSpy 等价物
【发布时间】:2021-11-26 23:45:20
【问题描述】:

我在我的应用程序中从 jasmine 迁移到 jest。我有以下行要测试

JSON.parse(window.document.querySelector(SELECTOR).innerHTML)

在我的测试中,我使用了茉莉花。

document.querySelector = jasmine.createSpy('HTML Element').and.returnValue(dummyEl)

但现在开玩笑我得到以下错误

TypeError: Cannot read property 'innerHTML' of null

你能帮帮我吗?

【问题讨论】:

    标签: jestjs jasmine


    【解决方案1】:

    我认为您需要jest.fn.mockReturnValue 的组合。

    我不擅长jest,但我认为可以:

    document.querySelector = jest.fn().mockReturnValue(dummyEl);
    

    在此处查看文档:

    https://jestjs.io/docs/jest-object#jestfnimplementation

    https://jestjs.io/docs/mock-function-api#mockfnmockimplementationfn

    【讨论】:

    • 我使用了 spy 并且它也有效。 jest.spyOn(document, 'querySelector').mockReturnValue(dummyEl);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-08-07
    • 2020-01-27
    • 2018-06-11
    • 1970-01-01
    • 2020-11-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多