CodeceptJS PostgreSQL Helper

1.0.1 · active · verified Wed Apr 22

CodeceptJS helper (version 1.0.1) designed to streamline end-to-end testing against PostgreSQL databases. This package allows developers to execute raw SQL queries directly from their CodeceptJS test scenarios, which is crucial for setting up test data, verifying database states, and cleaning up after tests. It integrates into the CodeceptJS ecosystem by being configured in `codecept.conf.js` and exposing its methods via the framework's actor object (`I`). This helper differentiates itself from general PostgreSQL client libraries by providing a test-centric interface within the CodeceptJS testing paradigm. The project is maintained by Percona-Lab, with its release cadence currently driven by community contributions and specific project needs.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to configure the `PostgresqlDBHelper` in a `codecept.conf.js` file, utilizing environment variables for secure credential management. It then provides an example CodeceptJS scenario (`my_db_test.js`) that uses the `I.runQuery` method to perform a full database test lifecycle: creating a temporary table, inserting test data, verifying the insertion, and finally cleaning up the table, ensuring isolated tests.

const { setHeadlessWhen, setWindowSize } = require('@codeceptjs/configure');
setHeadlessWhen(process.env.CI);
setWindowSize(1200, 800);

exports.config = {
  tests: './*_test.js',
  output: './output',
  helpers: {
    PostgresqlDBHelper: {
      require: 'codeceptjs-postgresqlhelper',
      host: process.env.DB_HOST ?? '127.0.0.1',
      port: parseInt(process.env.DB_PORT ?? '5432'),
      user: process.env.DB_USER ?? 'postgres',
      password: process.env.DB_PASSWORD ?? 'postgres',
      database: process.env.DB_DATABASE ?? 'testdb',
    },
    // Include another helper if browser interaction is needed, e.g., Playwright
    // Playwright: {
    //   url: 'http://localhost',
    //   show: true,
    //   browser: 'chromium'
    // }
  },
  include: {},
  bootstrap: async () => {
    // Optional: run initial setup like creating the database if it doesn't exist
    // (requires a separate connection for initial DB creation)
  },
  teardown: null,
  mocha: {},
  name: 'codeceptjs-postgresqlhelper-quickstart',
  plugins: {
    retryFailedStep: { enabled: true },
    screenshotOnFail: { enabled: true }
  }
};

// --- my_db_test.js ---
Feature('Database Operations');

Scenario('should create a table, insert data, and verify', async ({ I }) => {
  const tableName = 'test_users_' + Date.now();
  const username = 'john_doe_' + Date.now();
  const email = username + '@example.com';

  // 1. Create a table
  await I.runQuery(`CREATE TABLE IF NOT EXISTS ${tableName} (id SERIAL PRIMARY KEY, username VARCHAR(255) UNIQUE, email VARCHAR(255));`);
  console.log(`Table ${tableName} created or already exists.`);

  // 2. Insert data
  await I.runQuery(`INSERT INTO ${tableName} (username, email) VALUES ('${username}', '${email}');`);
  console.log(`Inserted user ${username}.`);

  // 3. Verify data insertion
  const selectResult = await I.runQuery(`SELECT * FROM ${tableName} WHERE username = '${username}';`);
  I.assert(selectResult.rows.length).equals(1);
  I.assert(selectResult.rows[0].email).equals(email);
  console.log(`Verified user ${username} in table.`);

  // 4. Clean up (optional, but good practice for isolated tests)
  await I.runQuery(`DROP TABLE ${tableName};`);
  console.log(`Table ${tableName} dropped.`);
});

view raw JSON →