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
相关产品推荐
相关产品推荐

