cy.getBy*** 不是函数

cy.getBy*** is not a function

我正在尝试 运行 Cypress 在我的 Azure 发布管道中。我安装了我在本地安装的所有软件包,但是当我 运行 赛普拉斯测试时,我遇到了几个错误:

TypeError: cy.visit(...).getByText is not a function

TypeError: cy.getByLabelText is not a function

TypeError: cy.getByPlaceholderText is not a function

我已将以下包添加到我的 package.json devDependencies 部分:

"@testing-library/cypress": "^4.0.4",
"@testing-library/react": "^8.0.4",
"@testing-library/dom": "latest",

知道 Cypress 返回这些类型错误的原因吗?

这是笔试的例子:

it("can request to join private team", () => {
  const privateTeamId = "fe1fa897-2e90-4ecb-91f9-0c9bb33ef63a";
  cy.get(`[id=${privateTeamId}]`)
    .click()
    .getByText("Request membership")
    .click()
    .getByText("Membership request sent");
});

您需要扩展 Cypress 的 cy 命令。

只需将此 import '@testing-library/cypress/add-commands'; 行添加到项目的 cypress/support/commands.js

添加此行后,如果仍然不起作用,请关闭 Cypress 并重新启动它。

在我的例子中,我发现 getBy* 查询在 Testing Library + Cypress 之间的集成中不存在。

所以我刚刚从 cy.getByText 迁移到 cy.findByText,一切正常。

根据文档:

Note: the get* queries are not supported because for reasonable Cypress tests you need retryability and find* queries already support that. query* queries are no longer necessary since v5 and will be removed in v6. find* fully support built-in Cypress assertions (removes the only use-case for query*).

https://testing-library.com/docs/cypress-testing-library/intro