【问题标题】:How can I import xhr-mock module right?如何正确导入 xhr-mock 模块?
【发布时间】:2022-02-07 14:27:00
【问题描述】:

xhr-mock 导入有问题(我正在使用 Jest 框架)。我有一个练习,我必须测试一个使用XMLHttprequest 的简单函数。为了做到这一点,我使用xhr-mock 模块并使用'setup' 方法来用MockXMLHttpRequest 替换全局XMLHttpRequest 对象。当我从测试中调用“设置”方法时,我收到错误:mockXHR.setup 不是函数。下面是我的代码。

const mockXHR = require('xhr-mock');
const { exampleFunction } = require('../function/exampleXHR');

describe('exampleXHR', () => {
    beforeEach(() => mockXHR.setup());

    test('xhr test', () => {
        exampleFunction();
    });

});

【问题讨论】:

    标签: javascript unit-testing jestjs mocking xmlhttprequest


    【解决方案1】:

    对于 commonJS,像这样使用它:

    var xhrMock = require("xhr-mock").default;
    
    console.log(xhrMock.setup)
    

    见:https://runkit.com/embed/gyrys612q3a2

    【讨论】:

      猜你喜欢
      • 2021-03-30
      • 2021-02-04
      • 1970-01-01
      • 2020-01-10
      • 2021-03-13
      • 2019-08-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多