How to configure cypress-sql-server with no cypress.json?

Viewed 18

I'm trying to setup cypress-sql-server, but I'm using version 10.8.0, which does not use cypress.json to configure the environment. All of the setup instructions I've found refer to using cypress.json to configure the plug-in. How do I either:

a) Get my cypress.config.js file to also use a cypress.json for configuration?

b) Put the db config into my cypress.config?

or

c) What config file should I put this in?

DB config example:

"db": {
    "userName": "user",
    "password": "pass",
    "server": "myserver",
    "options": {
        "database": "mydb",
        "encrypt": true,
        "rowCollectionOnRequestCompletion" : true
    }
}

My versions:

\cypress> npx cypress --version
Cypress package version: 10.8.0
Cypress binary version: 10.8.0
Electron version: 19.0.8
Bundled Node version:
16.14.2
2 Answers

cypress.json is a way to specify Cypress environment variables. Instead of using a cypress.json file, you can use any of the strategies in that link.

If you just wanted to include them in your cypress.config.js, it would look something like this:

const { defineConfig } = require('cypress')

module.exports = defineConfig({
  e2e: {
    baseUrl: 'http://localhost:1234',
    env: {
      db: {
        // your db values here
      }
    }
  }
})

This is the install instruction currently given by cypress-sql-server for Cypress v9

Plugin file

The plug-in can be initialised in your cypress/plugins/index.js file as below.

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

module.exports = (on, config) => {
  tasks = sqlServer.loadDBPlugin(config.db);
  on('task', tasks);
}

Translating that into Cypress v10+

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

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

      // allows db data to be accessed in tests
      config.db = {
        "userName": "user",
        "password": "pass",
        "server": "myserver",
        "options": {
          "database": "mydb",
          "encrypt": true,
          "rowCollectionOnRequestCompletion": true
        }
      }

      // code from /plugins/index.js
      tasks = sqlServer.loadDBPlugin(config.db);
      on('task', tasks);

      return config
    },
  },
})

Other variations work, such as putting the "db": {...} section below the "e2e: {...}" section, but not in the "env": {...} section.

Custom commands

Instructions for Cypress v9

Commands file

The extension provides multiple sets of commands. You can import the ones you need.
Example support/index.js file.

import sqlServer from 'cypress-sql-server';
sqlServer.loadDBCommands(); 

For Cypress v10+

Just move this code to support/e2e.js

Related