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

使用Cypress连接SQL Server失败,求可用配置示例

解决Cypress连接SQL Server的配置问题

你的配置核心问题:

  • cypress.config.ts里导出了两个defineConfig,后一个会覆盖前一个,导致SQL相关配置完全没生效
  • 数据库配置同时混用Windows认证(trusted_connection: true)和SQL账号认证(userName/password),二者只能选其一
  • 插件文件没有集成SQL Server的task,无法注册数据库操作命令

可用的完整配置示例:

1. cypress.config.ts

合并所有配置,只保留一个defineConfig导出:

import { defineConfig } from "cypress";
import sqlServer from "cypress-sql-server";

export default defineConfig({  
  chromeWebSecurity: false,
  videosFolder: 'cypress/videos',
  screenshotsFolder: 'cypress/screenshots',
  fixturesFolder: 'cypress/fixtures',
  video: false,
  reporter: 'cypress-mochawesome-reporter',
  reporterOptions: {
    reportDir: 'cypress/reports',
    charts: true,
    reportPageTitle: 'xxxxxxxx',
    embeddedScreenshots: true,
    inlineAssets: true,
  },  
  e2e: {
    experimentalSessionAndOrigin: true,
    specPattern: 'cypress/e2e/tests/orders/*',
    baseUrl: 'http://localhost:4200',
    setupNodeEvents(on, config) {
      // 配置数据库连接(二选一:SQL账号/Windows认证)
      // 方式1:SQL账号认证
      config.db = {
        userName: "你的SQL账号",
        password: "你的SQL密码",
        server: "xxx\\SQLEXPRESS",
        options: {
          database: "你的数据库名",
          encrypt: false, // 本地SQLEXPRESS通常不需要加密,生产环境再开启
          rowCollectionOnRequestCompletion: true,
          trusted_connection: false // 关闭Windows认证
        }
      };

      // 方式2:Windows认证(不需要userName和password)
      // config.db = {
      //   server: "xxx\\SQLEXPRESS",
      //   options: {
      //     database: "你的数据库名",
      //     encrypt: false,
      //     rowCollectionOnRequestCompletion: true,
      //     trusted_connection: true
      //   }
      // };

      // 加载SQL Server插件并注册task
      const sqlTasks = sqlServer.loadDBPlugin(config.db);
      on('task', sqlTasks);

      // 加载其他插件
      require('@cypress/code-coverage/task')(on, config);
      require('cypress-mochawesome-reporter/plugin')(on);  

      return config;
    },
  },
});

2. cypress/plugins/index.ts

如果保留单独的插件文件,需要把SQL task整合进来(或者直接把插件逻辑移到cypress.config.ts的setupNodeEvents里更简洁):

const sqlServer = require("cypress-sql-server");

module.exports = (on, config) => {
  // 加载SQL Server task
  const sqlTasks = sqlServer.loadDBPlugin(config.db);
  on('task', sqlTasks);

  // 其他插件
  require('@cypress/code-coverage/task')(on, config);
  require('cypress-mochawesome-reporter/plugin')(on);  

  return config;
};

3. cypress/support/e2e.ts

修正语法错误(多余的大括号),正确加载SQL命令:

import '@cypress/code-coverage/support';
import './commands';
import 'cypress-mochawesome-reporter/register';
import sqlServer from 'cypress-sql-server';

sqlServer.loadDBCommands();

测试用例示例

在你的测试文件中可以这样调用数据库操作:

it('查询数据库数据', () => {
  cy.sqlServer('SELECT TOP 1 * FROM 你的表名').then((result) => {
    expect(result.length).to.be.greaterThan(0);
    console.log('查询结果:', result);
  });
});

额外排查点

  • 确认SQL Server服务已启动,本地SQLEXPRESS默认允许本地连接,若需远程需额外配置
  • 检查服务器名称是否正确:可在SQL Server Management Studio中查看,格式通常为计算机名\SQLEXPRESS或localhost\SQLEXPRESS
  • 若使用SQL账号,确认账号拥有目标数据库的读写权限

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.15 10:55:28