使用 cy.request 以外的东西在 Cypress 中播种数据

Using something other than cy.request to seed data in Cypress

我正在使用 Cypress 进行端到端测试。在我的 beforeEach 中,我正在使用提供给我的 SDK 在服务器上播种数据(SDK 向服务器发送 API 调用但不在其中使用 cy.request) . SDK 上的方法 return 是一个承诺,因此我想我可以 return 这样的承诺:

beforeEach(() => {
  return sdk.createProperty(...);
});

然后我的测试会执行如下操作:

it('displays a property', () => {
    cy.visit(`/companies/${appTestData.companyId}/properties`);
    ...the rest is commented out currently...
}

这实际上按预期工作,也就是说,它在 运行 测试之前等待服务器响应 returned,但是当测试实际运行时,我在控制台中看到以下警告:

Cypress Warning: Cypress detected that you returned a promise in a test, but also invoked one or more cy commands inside of that promise...

我注意到如果我将 beforeEach 更改为使用 cy.then,警告就会消失:

beforeEach(() => {
  cy.then(() => sdk.createProperty(...));
});

这似乎有点不必要,有点像在黑暗中刺伤,所以我想知道是否有规定的方法来做我需要做的事情。我无法更改我正在使用的 SDK 以使用 cy.request,我认为这也会阻止警告。谢谢。

可能不是您想听到的,但我可以向您确认使用 cy.then(...) 是我所知道的在 Cypress 中等待 Promise 的最标准方式。

阅读您的问题后,我尝试使用 Cypress Network Requests 功能在 before() 中等待 fetch('my/url'),但它似乎没有检测到请求完全没有。