How do I configure cypress-sql-server with no cypress.json?

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. I am running into an error:

tasksqlServer:execute, SELECT 'Bob'
CypressError
cy.task('sqlServer:execute') failed with the following error:

The 'task' event has not been registered in the setupNodeEvents method. You must register it before using cy.task()

Fix this in your setupNodeEvents method here:
D:gitmcare.automationclientcypresscypress.config.jsLearn more
node_modules/cypress-sql-server/src/commands/db.js:7:1
   5 |     }
   6 | 
>  7 |     cy.task('sqlServer:execute', query).then(response => {
     | ^
   8 |       let result = [];
   9 | 

cypress.config.js

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
      const tasks = sqlServer.loadDBPlugin(config.db);
      on('task', tasks);

      return config      
      // implement node event listeners here
    },
  },
});

testSQL.spec.js

describe('Testing SQL queries', () => {
    
    it("It should return Bob", () => {
        cy.sqlServer("SELECT 'Bob'").should('eq', 'Bob');
        
});
})

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
``

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
      const 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

6

For cypress 10+ in config file:

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

    //process.env.server with npm i dotenv can be used for sensitive data in loadDBPlugin properties

    const yourDatabaseName = sqlServer.loadDBPlugin({
      server: "",
      port: ,
      userName: "",
      password: "",
      options: {
        database: "",
        encrypt: true,
        rowCollectionOnRequestCompletion: true
      }
    })
    
    on("task", {
          'sqlServer:execute': (sql) => yourDatabaseName['sqlServer:execute'](sql)
         })

in support/e2e.js file :

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

in test:

cy.sqlServer(`SELECT * FROM table WHERE id=''
      ).then((result) => {
        console.log(result)
      })

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
      }
    }
  }
})

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật