Registry / testing / pgtest

pgtest

JSON →
library0.2.3jsnpmunverified

pgtest is a drop-in replacement for node-postgres (pg) intended for unit testing. It allows you to mock database queries by specifying expected SQL strings or regular expressions, along with custom return data or errors. The current version is 0.2.3, and the library has seen no recent updates (last release appears years ago). It works with callbacks only, relying on rewire or similar tools to inject the mock into modules under test. Key differentiators: simple API, inline expectations without a separate query builder, and built-in verify/check to ensure all expectations were consumed. However, it does not support promises or async/await, and is not actively maintained, making it suitable only for legacy callback-based codebases.

npm install pgtest
INSTALL
IMPORT
SIG · PGTEST
P
pgtest
testingjavascriptv0.2.3
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

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

pgtest (default)
var pgtest = require('pgtest');
import pgtest from 'pgtest';
This package uses CommonJS and does not provide ES module exports. Use require() in Node.js environments.
pgtest.connect
pgtest.connect(db, callback);
pgtest.connect(db).then(callback);
connect() does not return a Promise; it invokes the callback immediately with a mocked client.
pgtest.expect
pgtest.expect('SELECT 1').returning(null, [{col: 'val'}]);
pgtest.expect(/SELECT/).returning(null, {rows: [{col: 'val'}]});
The .returning() data should be an array of rows, not wrapped in {rows: ...}. The library internally adds the rows property.

Demonstrates basic usage: set an expected SQL query with return data, run it through a mocked client, and verify expectations.

var pgtest = require('pgtest'); // Set up expected query pgtest.expect('SELECT * FROM vegetables').returning(null, [ ['potatoes', '1kg'], ['tomatoes', '500g'] ]); // Connect and run query pgtest.connect('foo', function (err, client, done) { client.query('SELECT * FROM vegetables', function (err, data) { console.log(data); // { rows: [ ['potatoes','1kg'], ['tomatoes','500g'] ] } done(); }); }); // Verify all expectations were met pgtest.check();
Debug
Known issues
deprecatedpgtest does not support Promises or async/await. Queries use callbacks only.
fix
Consider using a more modern PostgreSQL mock library like pg-mem, mock-knex, or sinon for promise-based testing.
affects: <=0.2.3
gotchaCalling pgtest.check() without resetting between tests may cause false negatives if expectations from previous tests remain.
fix
Call pgtest.reset() before each test case, e.g., in a beforeEach hook.
affects: *
gotchaExpected queries must be registered in the exact order they will be executed. pgtest does not support unordered matching.
fix
Register expectations in the same order queries are called in your code.
affects: *
gotchaclient.query() callback receives data as { rows: data } when using .returning(null, data). The data array is not the direct result.
fix
Access query results via result.rows, not result directly.
affects: *
Errors
Common errors & fixes
pgtest is not a function
Importing pgtest incorrectly (e.g., import pgtest from 'pgtest').
fix
Use var pgtest = require('pgtest');
Cannot find module 'pgtest'
pgtest is not installed.
fix
Run npm install pgtest --save-dev
client.query is not a function
Using a real pg client or a mock that doesn't have query method.
fix
Ensure you are using the client returned from pgtest.connect().
Upgrade
Version history
0.2.3latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
11 hits · last 30 days
node
10
Resources
packagepgtest
pgtest — npm install pgtest · libregistry