cypress `cy.now()` Type 'Promise<any>' has no call signatures

huangapple go评论70阅读模式
英文:

cypress `cy.now()` Type 'Promise<any>' has no call signatures

问题

Here's the translated content you requested:

我有这样的 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)

有任何提示吗?

英文:

I have such addQuery:

Cypress.Commands.addQuery(&#39;closePopup&#39;, (id) =&gt; {
const getClosePopup = cy.now(&#39;get&#39;,`[data-testid=&quot;${id}&quot;]`);
    return () =&gt; {
      return getClosePopup();
  }
});

And fro this i do get such error

This expression is not callable.
Not all constituents of type &#39;Promise&lt;any&gt; | ((subject: any) =&gt; any)&#39; are callable.
Type &#39;Promise&lt;any&gt;&#39; has no call signatures.ts(2349)

ANy hints on this :/?

答案1

得分: 3

以下是翻译好的内容:

这是因为 cy.now('get',[data-testid="${id}"]) 不返回一个函数。

您应该添加一个函数包装器:

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

或者直接从查询函数中返回表达式

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

实际上,您只需要一个 jQuery 表达式

Cypress.Commands.addQuery('closePopup', (id) => {
  return () => {
    return Cypress.$(`[data-testid="${id}"]`)
  }
})
英文:

That is because cy.now(&#39;get&#39;,[data-testid=&quot;${id}&quot;]) does not return a function.

You should add a function wrapper:

Cypress.Commands.addQuery(&#39;closePopup&#39;, (id) =&gt; {
  const getClosePopup = () =&gt; cy.now(&#39;get&#39;,`[data-testid=&quot;${id}&quot;]`);
  return () =&gt; {
      return getClosePopup();
  }
})

or return the expression directly from the query function

Cypress.Commands.addQuery(&#39;closePopup&#39;, (id) =&gt; {
  return () =&gt; {
    return cy.now(&#39;get&#39;,`[data-testid=&quot;${id}&quot;]`)
  }
})

Really, you just need a jQuery expression

Cypress.Commands.addQuery(&#39;closePopup&#39;, (id) =&gt; {
  return () =&gt; {
    return Cypress.$(`[data-testid=&quot;${id}&quot;]`)
  }
})

huangapple
  • 本文由 发表于 2023年5月15日 04:15:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76249497.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定