【问题标题】:JEST unit testing for $.get request$.get 请求的 JEST 单元测试
【发布时间】:2021-04-25 23:22:25
【问题描述】:

我在 React 中有一个代码

$.get('/bin/data', (response) => {
            this.data = response;
        });

这是我的测试用例:

$document = {
            on: jest.fn().mockName('$document.on'),
        };

        expect(window.$).toBeUndefined(); 
        $ = jest.fn().mockName('$').mockReturnValue($document);
        $.get = () => {};
        jest.spyOn($, 'get').mockName('$.get').mockImplementation(response => jest.fn()); // not sure what to do here such that callback function is also covered

        window.$ = $;

一切正常,但没有覆盖回调函数。

【问题讨论】:

    标签: javascript reactjs unit-testing ecmascript-6 jestjs


    【解决方案1】:

    如果解决了就会得到。

    jest.spyOn($, 'get').mockName('$.get').mockImplementation((url, myMockFn) => {
                myMockFn({ name: 'Tom' });
            });
    

    【讨论】:

      猜你喜欢
      • 2018-10-17
      • 2016-12-25
      • 2015-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-16
      • 2020-08-04
      • 2022-01-02
      相关资源
      最近更新 更多