Reputation: 2431
Below is the index.js
code I am using to connect to a MySQL DB in my cypress test:
const mysql = require('mysql')
function queryTestDb(query, config) {
const connection = mysql.createConnection(config.env.db)
connection.connect()
return new Promise((resolve, reject) => {
connection.query(query, (error, results) => {
if (error) reject(error)
else {
connection.end()
return resolve(results)
}
})
})
}
module.exports = (on, config) => {
on('task', { queryDb: query => { return queryTestDb(query, config) }, });
require('cypress-grep/src/plugin')(config)
return config
}
Currently, my test use the DB credentials provided in cypress.json
on this line:
const connection = mysql.createConnection(config.env.db)
But I want the framework to run in different environments, as the database name is different.
I have already created qa.json
& staging.json
config files that store the DB credentials like so:
qa.json
:
{
"extends": "./cypress.json",
"baseUrl": "myUrl",
"env": {
"db": {
"host": "myHost",
"user": "myUser",
"password": "myPassword",
"database": "taltektc_qa"
}
}
}
staging.json
:
{
"extends": "./cypress.json",
"baseUrl": "myUrl",
"env": {
"db": {
"host": "myUrl",
"user": "myUser",
"password": "myPassword",
"database": "taltektc_stage"
}
}
}
Here is the command I am currently using to run the tests:
npx cypress open --config-file staging.json
I tried to update my index.js
below, but I get a Cypress is not defined
error message:
module.exports = (on, config) => {
on('task', { queryDb: query => { return queryTestDb(query, Cypress.config()) }, });
Can someone please tell me what changes are required in my index.js
so that I can specify which config file to use when making the DB connection?
Upvotes: 2
Views: 739
Reputation: 32118
In a Node plugin task, the config
parameter is equivalent to Cypress.config()
in the browser-side spec.
You should be getting the correct config resolved after --config-file staging.json
is applied, so the original code is all you need
module.exports = (on, config) => {
on('task', { queryDb: query => { return queryTestDb(query, config) }, });
You can check what has been resolved after opening the runner, under settings/configuration
Upvotes: 1