【发布时间】: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