【问题标题】:Why am I receiving this warning in a cypress task?为什么我在柏树任务中收到此警告?
【发布时间】:2018-10-01 22:26:11
【问题描述】:

我正在运行一项任务,以便在 before 挂钩中播种我的数据库。赛普拉斯抱怨

Cypress 警告:Cypress 检测到您在测试中返回了一个 Promise,但还在该 Promise 中调用了一个或多个 cy 命令。

这是任务

import { seed } from '../../../src/server/db/seed'

const pluginHandler = on => {
  on('task', {
    'seed:db': () => {
      return seed()
    }
  })
}

export default pluginHandler

这是种子函数

import { exec } from 'child_process'
import util from 'util'

const execP = util.promisify(exec)

export const seed = () => {
  // Drop notes.
  return execP('mongo starter_test --eval "db.notes.drop()"')
    .then(async () => {
      // Insert notes fixtures.
      await execP(
        'mongoimport --db starter_test --collection notes --file ./src/server/db/notes.json'
      )
    })
    .then(() => {
      return 0
    })
}

最后是测试,它还没有做任何事情

describe('My First Test', () => {
  before(async () => {
    await cy.task('seed:db')
  })

  it('Does not do much!', () => {
    cy.visit(Cypress.env('HOST'))
  })
})

据我所知,我没有像警告所暗示的那样在承诺中使用命令。

【问题讨论】:

  • 这是一个 JS 逻辑问题,而不是 DB 或其他任何东西。您的“之前”本身不是异步的,因此它只会在后台处理并等待该回调,它实际上不会像您期望的那样使用“异步”解决。您可能需要在回调完成后调用“完成”回调。你在使用摩卡咖啡吗?如果是,那么“完成”就是你要找的东西。
  • before 是异步的,在 Mocha 中通常可以是异步的,但 Cypress 不喜欢 await 语法。

标签: cypress


【解决方案1】:

据我所知,async 不应在 Cypress 内部使用 before 或任何其他命令 (unless you install a 3rd-party lib)。

请尝试before(() => cy.task('seed:db'))

【讨论】:

  • 是的,这行得通(你也可以在其他情况下使用cy.task(...).then(...),但它会被await 绊倒。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-07-27
  • 1970-01-01
  • 2011-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-08
相关资源
最近更新 更多