【问题标题】:node warnings: UnhandledPromiseRejectionWarning: TypeError: displayErrorFunction is not a function节点警告:UnhandledPromiseRejectionWarning:TypeError:displayErrorFunction 不是函数
【发布时间】: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


【解决方案1】:

确保任何测试用例都缺少该参数。

您也可以为displayErrorFunction 添加一个默认值。

(payload, redirectFunction, displayErrorFunction = ()=>{} ) => {

}

【讨论】:

  • 在我们的例子中,我们需要修改测试,因为该功能是强制性的,但是,如果它是可选的,这是一个绝妙的解决方案。您为我们指明了正确的方向,以检查该函数是否实际上是作为测试参数传递的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-27
  • 2018-12-28
  • 2014-07-21
  • 2018-03-23
  • 2021-09-20
  • 2020-07-24
相关资源
最近更新 更多