根据 Cypress 请求设置 cookie 和 return 用户

Set cookies and return user on Cypress request

问题

我有一个 Cypress 命令,我可以在其中使用随机用户登录。 API 将 return 以下响应:

{
  user: { ... }
  token: { ... }
}

我想做的是:

  1. 使用 cy.request
  2. 创建用户
  3. 在浏览器中设置cookie
  4. Return 命令外的响应,以便我可以在命令之外使用它

我试过的

return cy.request({
    method: 'POST',
    url: getApiUrl('__cypress__/login'),
    body: requestBody,
    log: false,
  })
    .then(({ body }) => {
      cy
        .setCookie('_token', body.token.plainTextToken)
        .then(() => {
          Cypress.log({
            name: 'login',
            message: JSON.stringify(body),
            consoleProps: () => ({ user: body }),
          });
        });
    })
    .its('body', { log: false })  times out here

我正在寻找的是做类似的事情:

cy.login().then(({ user }) => { 
  // use logged in user
})

问题

Cypress 在 .its(...) 行超时。这可能吗?查看 docs 我找不到任何关于我要实现的目标的示例

(来自评论)

发生这种情况是因为之前链接的主题没有 return 任何东西。 body 属性 的显式 return 将修复它。