Reputation: 101
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:\git\mcare.automation\client\cypress\cypress.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
``
Upvotes: 4
Views: 2865
Reputation: 1
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)
})
Upvotes: 0
Reputation: 32118
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.
Instructions for Cypress v9
Commands file
The extension provides multiple sets of commands. You can import the ones you need.
Examplesupport/index.js
file.import sqlServer from 'cypress-sql-server'; sqlServer.loadDBCommands();
For Cypress v10+
Just move this code to support/e2e.js
Upvotes: 7
Reputation: 7164
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
}
}
}
})
Upvotes: -1