小编Ben*_*n R的帖子

我如何使用 jest 模拟这个 http 请求?

我刚开始使用 Jest 进行单元测试。我如何模拟这个简单的http请求方法“getData”?这是课程:

const got = require("got")

class Checker {


    constructor() {
        this.url

        this.logData = this.logData.bind(this);
        this.getData = this.getData.bind(this);

    }

    async getData(url) {
        const response = await got(url);
        const data = await response.body;
        return data;
    }

    async logData(first, second, threshold) {
        
        let data = await this.getData(this.url)
        
        console.log("received " + data.body);

    }

}
Run Code Online (Sandbox Code Playgroud)

我正在尝试模拟“getData”,以便我可以为“logData”编写单元测试。我需要模拟整个“got”模块吗?谢谢。

unit-testing asynchronous mocking node.js jestjs

9
推荐指数
2
解决办法
4万
查看次数

标签 统计

asynchronous ×1

jestjs ×1

mocking ×1

node.js ×1

unit-testing ×1