Cypress PostgreSQL Database Integration
This package provides a set of Cypress commands and plugin utilities for interacting directly with a PostgreSQL database during end-to-end tests. It allows test suites to execute SQL queries, verify data, or seed test data by leveraging the `pg` client library. The current stable version is 1.0.8. Based on the README, it appears to support older Cypress plugin architectures (e.g., `cypress/plugins/index.js`), which suggests it is not actively maintained for the latest Cypress versions (Cypress 10+ uses `cypress.config.js`). Its primary differentiation is enabling direct database interaction within Cypress tests, streamlining data setup and verification workflows, offering a direct bridge for database operations within the Cypress testing environment, which can be critical for test data management and assertions.
Common errors
-
TypeError: postgreSQL.loadDBPlugin is not a function
cause The `cypress-postgresql` module was imported incorrectly, or the `pg` dependency is missing.fixEnsure `pg` is installed (`npm install pg`) and that `cypress-postgresql` is `require`d in the plugin file (e.g., `const postgreSQL = require('cypress-postgresql');`) as it exposes `loadDBPlugin` as a method on its default export. -
Cypress encountered an error while connecting to the database. (connection refused)
cause The PostgreSQL server is not running, or the database connection details in `cypress.json` (host, port, user, password, database) are incorrect or inaccessible from the Cypress test environment.fixVerify that your PostgreSQL server is running and accessible from where Cypress tests are executed. Double-check all database credentials in your `cypress.json` file for accuracy. Ensure no firewall rules are blocking the connection. -
error: relation "your_table_name" does not exist
cause The SQL query being executed refers to a table or column that does not exist in the connected PostgreSQL database, or the database user lacks permissions to access it.fixReview your SQL query for typos in table or column names. Confirm that the table and columns exist in the target database. Check the database user's permissions to ensure they have read/write access to the necessary tables.
Warnings
- breaking This package relies on the deprecated `cypress/plugins/index.js` file for configuration. It is not compatible with Cypress 10.0+ out-of-the-box, which introduced `cypress.config.js` and a new configuration API. Migration requires significant manual effort.
- gotcha The `pg` (node-postgres) package is a direct dependency for the database connection and must be installed separately by the user. It is not bundled or listed as a peer dependency in the provided metadata.
- gotcha Storing database credentials directly in `cypress.json` (as suggested by the README) is a security risk, especially if the file is committed to version control. Sensitive information should be handled securely.
- deprecated The package's primary integration method through `cypress/plugins/index.js` is considered a legacy approach in modern Cypress versions. While commands in `cypress/support/index.js` remain viable, the plugin side is outdated.
Install
-
npm install cypress-postgresql -
yarn add cypress-postgresql -
pnpm add cypress-postgresql
Imports
- postgreSQL
import { postgreSQL } from 'cypress-postgresql';import postgreSQL from 'cypress-postgresql';
- loadDBPlugin
import { loadDBPlugin } from 'cypress-postgresql';const postgreSQL = require('cypress-postgresql'); // then tasks = postgreSQL.loadDBPlugin(pool); - loadDBCommands
import { loadDBCommands } from 'cypress-postgresql';import postgreSQL from 'cypress-postgresql'; postgreSQL.loadDBCommands();
Quickstart
/* cypress.json or cypress.config.js equivalent */
{
"db": {
"user": "process.env.PG_USER ?? ''",
"password": "process.env.PG_PASSWORD ?? ''",
"host": "localhost",
"port": 5432,
"database": "test_db"
}
}
/* cypress/plugins/index.js (Cypress < 10) or cypress.config.js setup */
const postgreSQL = require('cypress-postgresql');
const { Pool } = require('pg');
const dbConfig = require('../../cypress.json'); // Adjust path as needed
module.exports = (on, config) => {
const pool = new Pool(dbConfig.db);
const tasks = postgreSQL.loadDBPlugin(pool);
on('task', tasks);
return config;
};
/* cypress/support/commands.js */
import postgreSQL from 'cypress-postgresql';
postgreSQL.loadDBCommands();
/* cypress/e2e/db.cy.js */
describe('Database interactions', () => {
beforeEach(() => {
// Ensure the database is clean or seeded before each test
cy.postgresql('TRUNCATE TABLE users RESTART IDENTITY;').then(() => {
cy.log('Users table truncated.');
});
});
it('should insert and retrieve data from PostgreSQL', () => {
cy.postgresql(`INSERT INTO users (name, email) VALUES ('John Doe', 'john@example.com');`)
.then(() => {
return cy.postgresql(`SELECT name FROM users WHERE email = 'john@example.com';`);
})
.then((result) => {
expect(result[0].name).to.eq('John Doe');
});
});
});