{"id":43617,"library":"pgtest","title":"pgtest","description":"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.","status":"maintenance","version":"0.2.3","language":"javascript","source_language":"en","source_url":"https://github.com/thepeak99/node-postgres-test","tags":["javascript","postgres","postgresql","node","node-postgres","test"],"install":[{"cmd":"npm install pgtest","lang":"bash","label":"npm"},{"cmd":"yarn add pgtest","lang":"bash","label":"yarn"},{"cmd":"pnpm add pgtest","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"This package uses CommonJS and does not provide ES module exports. Use require() in Node.js environments.","wrong":"import pgtest from 'pgtest';","symbol":"pgtest (default)","correct":"var pgtest = require('pgtest');"},{"note":"connect() does not return a Promise; it invokes the callback immediately with a mocked client.","wrong":"pgtest.connect(db).then(callback);","symbol":"pgtest.connect","correct":"pgtest.connect(db, callback);"},{"note":"The .returning() data should be an array of rows, not wrapped in {rows: ...}. The library internally adds the rows property.","wrong":"pgtest.expect(/SELECT/).returning(null, {rows: [{col: 'val'}]});","symbol":"pgtest.expect","correct":"pgtest.expect('SELECT 1').returning(null, [{col: 'val'}]);"}],"quickstart":{"code":"var pgtest = require('pgtest');\n\n// Set up expected query\npgtest.expect('SELECT * FROM vegetables').returning(null, [\n  ['potatoes', '1kg'],\n  ['tomatoes', '500g']\n]);\n\n// Connect and run query\npgtest.connect('foo', function (err, client, done) {\n  client.query('SELECT * FROM vegetables', function (err, data) {\n    console.log(data); // { rows: [ ['potatoes','1kg'], ['tomatoes','500g'] ] }\n    done();\n  });\n});\n\n// Verify all expectations were met\npgtest.check();","lang":"javascript","description":"Demonstrates basic usage: set an expected SQL query with return data, run it through a mocked client, and verify expectations."},"warnings":[{"fix":"Consider using a more modern PostgreSQL mock library like pg-mem, mock-knex, or sinon for promise-based testing.","message":"pgtest does not support Promises or async/await. Queries use callbacks only.","severity":"deprecated","affected_versions":"<=0.2.3"},{"fix":"Call pgtest.reset() before each test case, e.g., in a beforeEach hook.","message":"Calling pgtest.check() without resetting between tests may cause false negatives if expectations from previous tests remain.","severity":"gotcha","affected_versions":"*"},{"fix":"Register expectations in the same order queries are called in your code.","message":"Expected queries must be registered in the exact order they will be executed. pgtest does not support unordered matching.","severity":"gotcha","affected_versions":"*"},{"fix":"Access query results via result.rows, not result directly.","message":"client.query() callback receives data as { rows: data } when using .returning(null, data). The data array is not the direct result.","severity":"gotcha","affected_versions":"*"}],"env_vars":null,"search_vec":"'0.2.3':44 'activ':110 'ago':57 'allow':19 'along':33 'api':80 'appear':55 'async/await':106 'base':120 'builder':87 'built':90 'built-in':89 'callback':61,119 'callback-bas':118 'codebas':121 'consum':98 'current':41 'custom':35 'data':37 'databas':23 'differenti':78 'drop':6 'drop-in':5 'ensur':94 'error':39 'expect':27,82,96 'express':32 'howev':99 'inject':70 'inlin':81 'intend':14 'javascript':122 'key':77 'last':53 'legaci':117 'librari':47 'maintain':111 'make':112 'mock':22,72 'modul':74 'node':11,125,127 'node-postgr':10,126 'pg':13 'pgtest':1,2 'postgr':12,123,128 'postgresql':124 'promis':104 'queri':24,86 'recent':51 'regular':31 'releas':54 'reli':63 'replac':8 'return':36 'rewir':65 'seen':49 'separ':85 'similar':67 'simpl':79 'specifi':26 'sql':28 'string':29 'suitabl':114 'support':103 'test':17,76,129 'tool':68 'unit':16 'updat':52 'verify/check':92 'version':42 'without':83 'work':59 'year':56","created_at":"2026-06-05T17:00:39.472888+00:00","updated_at":"2026-06-05T17:00:39.472888+00:00","problems":[{"fix":"Use var pgtest = require('pgtest');","cause":"Importing pgtest incorrectly (e.g., import pgtest from 'pgtest').","error":"pgtest is not a function"},{"fix":"Run npm install pgtest --save-dev","cause":"pgtest is not installed.","error":"Cannot find module 'pgtest'"},{"fix":"Ensure you are using the client returned from pgtest.connect().","cause":"Using a real pg client or a mock that doesn't have query method.","error":"client.query is not a function"}],"ecosystem":"npm","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":null,"cli_version":null,"type":"library","homepage":"https://github.com/thepeak99/node-postgres-test","github":"https://github.com/thepeak99/node-postgres-test","docs":null,"changelog":null,"pypi":null,"npm":"pgtest","openapi_spec":null,"status_page":null,"smithery":null,"categories":["testing"],"base_url":null,"auth_type":null,"provenance":{"verified_status":null,"verified_at":null,"last_verified":"2026-06-05","next_check":"2026-09-03","install_tag":null}}