【发布时间】:2021-09-19 04:20:12
【问题描述】:
我们有两种代码变体,它们都返回相同的节点警告。
变体 1
import axios from 'axios';
const correctEndpoint = `https://${process.env.AUTH0_DOMAIN}/dbconnections/signup`;
const headers = {
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
Accept: '*/*'
}
};
const registerWithAuth0 = async (payload, redirectFunction, displayErrorFunction) => {
try {
const response = await axios.post(correctEndpoint, payload, headers);
if (response.status === 200) {
redirectFunction();
} else {
displayErrorFunction();
}
} catch (err) {
displayErrorFunction();
}
}
export default registerWithAuth0;
变体 2
import axios from 'axios';
const correctEndpoint = `https://${process.env.AUTH0_DOMAIN}/dbconnections/signup`;
const headers = {
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
Accept: '*/*'
}
};
const registerWithAuth0 = (payload, redirectFunction, displayErrorFunction) => {
axios.post(correctEndpoint, payload, headers)
.then((response) => {
if (response.status === 200) {
redirectFunction();
} else {
displayErrorFunction();
}
})
.catch (() => {
displayErrorFunction();
});
}
export default registerWithAuth0;
所有笑话测试都通过了,但我们可以看到以下一些节点警告:
(node:26886) UnhandledPromiseRejectionWarning: TypeError: displayErrorFunction is not a function
(Use `node --trace-warnings ...` to show where the warning was created)
(node:26886) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:26886) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
(node:26886) UnhandledPromiseRejectionWarning: TypeError: displayErrorFunction is not a function
(node:26886) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 3)
我们正在使用:
- 节点 v14.17.1
- 反应 v17.0.1
- 开玩笑 v26.6.3
- babel-jest v26.6.3
- @babel/core v7.14.0
对这里可能出现的问题有什么想法吗?
网上也有一些类似的问题被报道,但并不完全相同:
【问题讨论】:
-
你是否在测试用例中传递了
displayErrorFunction参数?如果不是,则将默认参数设为displayErrorFunction = ()=>{} -
谢谢@AritraChakraborty 解决了,有一个测试用例没有。
-
好的,我会将其添加为答案,以便其他人可以找到它。随意接受它@dankilev
标签: node.js reactjs jestjs babel-jest