gpt4 book ai didi

JavaScript - Jest/mock "was not called"中的模拟控制台

转载 作者:行者123 更新时间:2023-11-30 20:01:05 24 4
gpt4 key购买 nike

我正在尝试模拟 console.info,我知道它会在导入的函数运行时被调用。该函数完全由单个 fetch 组成,当未在生产环境中运行时,它会使用 console.info 报告请求和响应。

在问题Jest. How to mock console when it is used by a third-party-library? ,评分最高的答案建议覆盖 global.console,所以我使用 jest.spyOn尝试一下:

import * as ourModule from "../src/ourModule";

test("Thing", () => {
// Tested function requires this. Including it here in case it's causing
// something quirky that readers of this question may know about
global.fetch = require("jest-fetch-mock");

const mockInfo = jest.spyOn(global.console, "info").mockImplementation(
() => { console.error("mockInfo") }
);

ourModule.functionBeingTested("test");
expect(mockInfo).toHaveBeenCalled();
}

正如预期的那样,输出包含一个“mockInfo”实例。但是,然后使用 toHaveBeenCalled() 进行测试失败。

expect(jest.fn()).toHaveBeenCalled()

Expected mock function to have been called, but it was not called.

40 |
41 | ourModule.functionBeingTested("test");
> 42 | expect(mockInfo).toHaveBeenCalled();
| ^
43 |

at Object.toHaveBeenCalled (__tests__/basic.test.js:42:22)

console.error __tests__/basic.test.js:38
mockInfo

我已经尝试将 spyOn 移动到加载模块之前,正如答案评论之一所建议的那样,结果没有差异。我在这里缺少什么?

这里是有问题的函数:

function functionBeingTested(value) {
const fetchData = {
something: value
};

fetch("https://example.com/api", {
method: "POST",
mode: "cors",
body: JSON.stringify(fetchData),
})
.then( response => {
if (response.ok) {
if (MODE != "production") {
console.info(fetchData);
console.info(response);
}
} else {
console.error(`${response.status}: ${response.statusText}`);
}
})
.catch( error => {
console.error(error);
});
}

最佳答案

问题

console.info 在 Promise 回调中调用,该回调在 ourModule.functionBeingTested 返回且 expect 运行时尚未执行。

解决方案

确保调用 console.info 的 Promise 回调在运行 expect 之前已经运行。

最简单的方法是从 ourModule.functionBeingTested 返回 Promise:

function functionBeingTested(value) {
const fetchData = {
something: value
};

return fetch("https://example.com/api", { // return the Promise
method: "POST",
mode: "cors",
body: JSON.stringify(fetchData),
})
.then(response => {
if (response.ok) {
if (MODE != "production") {
console.info(fetchData);
console.info(response);
}
} else {
console.error(`${response.status}: ${response.statusText}`);
}
})
.catch(error => {
console.error(error);
});
}

...并在断言之前等待它解决:

test("Thing", async () => {  // use an async test function...
// Tested function requires this. Including it here in case it's causing
// something quirky that readers of this question may know about
global.fetch = require("jest-fetch-mock");

const mockInfo = jest.spyOn(global.console, "info").mockImplementation(
() => { console.error("mockInfo") }
);

await ourModule.functionBeingTested("test"); // ...and wait for the Promise to resolve
expect(mockInfo).toHaveBeenCalled(); // SUCCESS
});

关于JavaScript - Jest/mock "was not called"中的模拟控制台,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53376293/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com