cypress - 无论如何在cypress test中查询mongodb?

标签 cypress

我希望通过 cypress test 与 mongodb 交互。我找不到任何有用的文档。请帮助我如何实现这一点?

最佳答案

查看他们关于任务的文档:https://docs.cypress.io/api/commands/task.html#Command
我需要用 mongodb 做一些事情,我设法连接做这样的事情(在“插件”目录中):

const MongoClient = require('mongodb').MongoClient;
module.exports = (on, config) => {
  on('task', {
    updateTask (id) {
      return new Promise((resolve) => {
        MongoClient.connect('mongodb://localhost:27017', (err, client) => {
          if (err) {
            console.log(`MONGO CONNECTION ERROR: ${err}`)  
            throw err;
          } else {
            const db = client.db('myDB');
            db.collection('someCollection').count({}, function(error, numOfDocs){              
              resolve({success: numOfDocs})
              client.close();
             })
          }
        });
      }); // end of return Promise
    }
  }) // end of task
}
你在“规范”中调用它,如下所示:
cy.task('updateTask', someParam).then((textOrNull) => {   
  console.log(textOrNull) 
})

关于cypress - 无论如何在cypress test中查询mongodb?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59743577/

相关文章:

cypress - 如何让 Cypress 模拟 Hammer Tap?

javascript - Cypress .io : Anyway to test for specific scroll amount?

cucumber - 如何在 Cypress 功能文件中添加 Before() 函数?

cypress - 如何在 Cypress 测试中测试应用程序进行的 API 调用次数

javascript - Cypress 通过部分元素名称查找元素

javascript - Cypress:带有多个参数的 cy.task()

node.js - 如何在 Cypress 中共享变量值

cypress - 无法连接到总线 : Address does not contain a colon

javascript - commands.js 中未定义 Cypress 和 cy,但一切正常

javascript - 如何保存变量/文本以便稍后在 Cypress 测试中使用?