Registry / testing / cypress-postgresql

cypress-postgresql

JSON →
library1.0.8jsnpmunverified

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.

npm install cypress-postgresql
INSTALL
IMPORT
SIG · CYPRESS-POSTGRESQL
C
cypress-postgresql
testingjavascriptv1.0.8
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

postgreSQL
import postgreSQL from 'cypress-postgresql';
import { postgreSQL } from 'cypress-postgresql';
Used for loading Cypress custom commands. The package exports its main functionality as a default export.
loadDBPlugin
const postgreSQL = require('cypress-postgresql'); // then tasks = postgreSQL.loadDBPlugin(pool);
import { loadDBPlugin } from 'cypress-postgresql';
This function is exposed as a method on the default export for CommonJS environments (e.g., `cypress/plugins/index.js`).
loadDBCommands
import postgreSQL from 'cypress-postgresql'; postgreSQL.loadDBCommands();
import { loadDBCommands } from 'cypress-postgresql';
This function is exposed as a method on the default export for ES module environments (e.g., `cypress/support/index.js`).

This quickstart demonstrates how to configure `cypress-postgresql` in a Cypress setup (pre-Cypress 10.0), define database credentials, load custom commands, and execute a basic insert and select query within a test.

/* 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'); }); }); });
Debug
Known issues
breakingThis 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.
fix
For Cypress 10+, manual refactoring is needed to adapt the plugin logic to the `cypress.config.js` setup. This may involve using `defineConfig` and handling `on('task', ...)` directly within the configuration file or creating a custom `setupNodeEvents` function. Alternatively, consider using a more modern Cypress database plugin.
affects: >=1.0.0
gotchaThe `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.
fix
Ensure `npm install pg` or `yarn add pg` is run in your project's root directory alongside `cypress-postgresql`.
affects: >=1.0.0
gotchaStoring 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.
fix
Utilize environment variables (e.g., `process.env.PG_USER`) in your `cypress.json` or `cypress.config.js` for database credentials. Pass them during test execution, for example, using a `.env` file and a package like `dotenv` or directly in your CI/CD pipeline.
affects: >=1.0.0
deprecatedThe 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.
fix
For new projects, prefer Cypress 10+ and integrate database tasks directly into `cypress.config.js` or explore alternative, actively maintained Cypress database plugins that support the latest configuration patterns.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: postgreSQL.loadDBPlugin is not a function
The `cypress-postgresql` module was imported incorrectly, or the `pg` dependency is missing.
fix
Ensure `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)
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.
fix
Verify 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
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.
fix
Review 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.
Upgrade
Version history
1.0.8latest on npm
Audit
Dependencies
pgrequiredRequired as a peer dependency for database connection pooling and query execution within the Cypress plugin file.
Agent activity
13 hits · last 30 days
node
12
OpenAI (training)
1
Resources
cypress-postgresql — npm install cypress-postgresql · libregistry