PostgreSQL SQL Test Runner
pg-test is a focused command-line interface (CLI) utility designed for running tests directly from `.sql` files against a PostgreSQL database. As of version 1.0.7, it provides a straightforward mechanism for database-centric testing, executing each `.sql` file in a specified directory sequentially and stopping on the first failure. Its release cadence appears to be stable and utility-focused, without frequent major updates. Key differentiators include its simplicity, direct execution of SQL scripts, and reliance on a `DB` environment variable for database connection, making it suitable for CI environments like Travis-CI. Unlike typical JavaScript test runners, pg-test primarily operates on raw SQL files rather than integrating with JS/TS test frameworks.
Common errors
-
Error: Environment variable DB is not set
cause The required PostgreSQL connection URL was not provided via the `DB` environment variable.fixSet the `DB` environment variable with a valid PostgreSQL connection string before running `pg-test`. Example: `export DB=postgres://user:pass@host:port/dbname`. -
Error: connect ECONNREFUSED 127.0.0.1:5432
cause The `pg-test` utility could not establish a connection to the PostgreSQL server, likely due to the server not running, incorrect host/port, or firewall issues.fixVerify that your PostgreSQL server is running, the specified host and port in the `DB` environment variable are correct, and no firewall is blocking the connection. Check PostgreSQL logs for details. -
syntax error at or near "SELECT"
cause One of your `.sql` test files contains invalid SQL syntax.fixReview the SQL file where the error occurred. Correct the syntax. The error message usually provides clues about the line number or near where the error happened.
Warnings
- gotcha The `DB` environment variable is mandatory for `pg-test` to connect to PostgreSQL. If not set, the tool will likely fail with a connection error or use an unexpected default (like `travis` on Travis-CI).
- breaking pg-test is a CLI-first tool. There is no documented or intended programmatic API for `import`ing functions or classes directly from the package. Attempting to do so will result in import errors or undefined behavior.
- gotcha The runner stops on the first SQL file failure. This means it's not designed to report all failures in a suite but rather to immediately halt execution, which can be less informative for large test suites.
- gotcha pg-test executes raw SQL files. This limits its capabilities compared to full-fledged JavaScript/TypeScript test frameworks (e.g., dynamic test generation, complex assertions, mocking).
Install
-
npm install pg-test -
yarn add pg-test -
pnpm add pg-test
Imports
- pg-test
import { run } from 'pg-test';import { spawn } from 'child_process'; spawn('pg-test', ['path/to/tests'], { stdio: 'inherit' }); - PgTestRunner
import PgTestRunner from 'pg-test';
// No direct programmatic API documented for import.
- Configuration
import { configure } from 'pg-test';// Configuration is via environment variables or CLI arguments.
Quickstart
import { spawnSync } from 'child_process';
import path from 'path';
import fs from 'fs';
// Create a dummy test file
const testFilePath = path.join(process.cwd(), 'temp_test.sql');
fs.writeFileSync(testFilePath, 'SELECT 1 = 1; -- Should pass');
// Set the DB environment variable (replace with your actual DB URL)
// For local PostgreSQL without password: 'postgres://postgres@localhost/your_db_name'
// Make sure 'your_db_name' exists and is accessible.
process.env.DB = process.env.DB ?? 'postgres://postgres@localhost/testdb';
console.log(`Running pg-test against: ${process.env.DB}`);
const result = spawnSync('pg-test', [path.dirname(testFilePath)], {
stdio: 'inherit',
env: process.env
});
if (result.status !== 0) {
console.error('pg-test failed!');
} else {
console.log('pg-test completed successfully.');
}
// Clean up the dummy test file
fs.unlinkSync(testFilePath);