-3

I am trying to connect the DB for testing web elements that have dependencies on data, and in general to add some reliable BE testing.

Currently I have cypress.config.js:

// cypress.config.js

const { defineConfig } = require("cypress");
const sql = require('mssql');
const dbConfig = require("./db_config");

const TEST_CREDENTIALS = require("./cypress.env.json");

module.exports = defineConfig({
  projectId: "iu4xc2",

  e2e: {
    baseUrl: "",
    viewportWidth: 1400,   
    viewportHeight: 900,  

    setupNodeEvents(on, config) {


      on('task', {
        async testDbConnection() {
          let connectionStatus = false;
          try {
            console.log('Attempting to connect to the database...');
            await sql.connect(dbConfig)
            connectionStatus = true;
            console.log('✅ Database connection successful!');
            return connectionStatus
          } catch (err) {
            console.error('❌ Database Connection FAILED:', err.message);
            throw new Error(`DB Connection Error: ${err.message}`);
          } finally {
            sql.close();
            console.log('Connection closed.');
          }
        }
      })

.
.
.
    video: false,
    screenshotOnRunFailure: true,
    chromeWebSecurity: false,
    watchForFileChanges: false,
  },
});

and db_config:

// db_config.js
module.exports = {
    // Use the Server Name from your image
    server: '*******',
    user: '*******',
    password: '*************',
    database: '<default>',

    options: {
        encrypt: true,
        trustServerCertificate: true,
    }
};

I can't connect using SQL Server but same credentials connects with SSMS and with the SQL Server extension on VS Code as well.

What is going wrong here?

1
  • "I can't connect" is a very vague statement. What's the original error you're getting? Network, failed login, wrong database? Commented 2 days ago

1 Answer 1

0

Cypress + Node + mssql cannot auto-detect SQL Server ports or named instances, but SSMS can.
That’s why SSMS connects successfully but mssql fails.

db_config.js

// db_config.js
module.exports = {
    server: 'YOUR_SERVER_NAME',   // no named instances
    port: 1433,                   // REQUIRED
    user: 'YOUR_USERNAME',
    password: 'YOUR_PASSWORD',
    database: 'YOUR_DATABASE',

    options: {
        encrypt: false,                // set to false for local SQL Servers
        trustServerCertificate: true,  // required if SSL is not configured
    }
};

cypress.config.js

// cypress.config.js

const { defineConfig } = require("cypress");
const sql = require("mssql");
const dbConfig = require("./db_config");

module.exports = defineConfig({
  e2e: {
    setupNodeEvents(on, config) {

      on("task", {
        async testDbConnection() {
          try {
            console.log("Attempting database connection...");

            const pool = await sql.connect(dbConfig); // correct usage
            console.log("✅ Database connection successful!");

            await pool.close(); // correct cleanup
            console.log("Connection closed.");

            return true;
          } catch (err) {
            console.error("❌ Database connection error:", err.message);
            throw err;
          }
        }
      });

    }
  }
});



your text example

it("Database test", () => {
  cy.task("testDbConnection").should("equal", true);
});
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.