You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Cypress使用cy.now()遇Promise<any>无调用签名错误,求解决思路

问题

我编写了如下addQuery命令:

Cypress.Commands.addQuery('closePopup', (id) => {
const getClosePopup = cy.now('get',`[data-testid="${id}"]`);
    return () => {
      return getClosePopup();
  }
});

运行后出现如下错误:

This expression is not callable.
Not all constituents of type 'Promise<any> | ((subject: any) => any)' are callable.
Type 'Promise<any>' has no call signatures.ts(2349)
解决提示
  • 错误根源:cy.now()提前赋值时会直接执行命令并返回Promise,而你后续把这个Promise当成函数调用,触发了类型错误。addQuery要求返回的是可复用的查询函数,而非执行结果。
  • 修正写法1:在返回的函数内调用cy.now()
Cypress.Commands.addQuery('closePopup', (id) => {
  return () => {
    return cy.now('get', `[data-testid="${id}"]`)();
  }
});
  • 修正写法2:直接返回cy.now()生成的查询函数(更简洁)
Cypress.Commands.addQuery('closePopup', (id) => {
  return cy.now('get', `[data-testid="${id}"]`);
});

内容的提问来源于stack exchange,提问作者shivetay

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 03:44:55