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.

testing
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.

This package uses CommonJS and does not provide ES module exports. Use require() in Node.js environments.

var pgtest = require('pgtest');

connect() does not return a Promise; it invokes the callback immediately with a mocked client.

pgtest.connect(db, callback);

The .returning() data should be an array of rows, not wrapped in {rows: ...}. The library internally adds the rows property.

pgtest.expect('SELECT 1').returning(null, [{col: 'val'}]);

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 footguns
deprecatedpgtest does not support Promises or async/await. Queries use callbacks only.
gotchaCalling pgtest.check() without resetting between tests may cause false negatives if expectations from previous tests remain.
gotchaExpected queries must be registered in the exact order they will be executed. pgtest does not support unordered matching.
gotchaclient.query() callback receives data as { rows: data } when using .returning(null, data). The data array is not the direct result.
Upgrade
Version history

Breaking-change detection hasn't run for this library yet.

Audit
Security & dependencies

CVE tracking and dependency tree are planned for a later release.

Agent activity
9 hits · last 30 days
gptbot
3
bytedance
2
ahrefsbot
1
Resources
packagepgtest