You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为Cypress测试中cypress-sql-server发送的SQL请求增加默认超时?

解决cypress-sql-server大数据量查询超时问题

方法1:覆盖自定义命令支持超时参数

在cypress/support/commands.js中添加代码,扩展原sqlServer命令,使其支持传入timeout选项:

Cypress.Commands.overwrite('sqlServer', (originalFn, query, options = {}) => {
  const customTimeout = options.timeout || Cypress.config('defaultCommandTimeout');
  return originalFn(query).timeout(customTimeout);
});

之后即可按需求调用:

cy.sqlServer(query, { timeout: 90000 }).then(($data) => {
  console.log($data);
});

方法2:全局修改Task超时(影响所有Cypress任务)

若需要所有SQL查询统一使用超时配置,可在cypress.config.js中调整全局taskTimeout:

const { defineConfig } = require("cypress");
const sqlServer = require('cypress-sql-server');

module.exports = defineConfig({
  e2e: {
    setupNodeEvents(on, config) {
      sqlServer(on, config);
      // 设置所有task超时为90秒
      config.taskTimeout = 90000;
      return config;
    },
  },
});

方法3:链式调用timeout命令(临时快速方案)

无需修改配置,直接在调用时链式添加超时设置:

cy.sqlServer(query).timeout(90000).then(($data) => {
  console.log($data);
});

内容的提问来源于stack exchange,提问作者vitaliy4us

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 20:42:11