npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

pgtest

v0.2.3

Published

A module that provides easy unit-testing for the popular node-postgres.

Downloads

1,916

Readme

node-postgres-test

Build Status

node-postgres-test is a module that provides easy unit testing for the popular node-postgres.

Install

npm install pgtest

Example


pgtest = require('pgtest');

pgtest.expect('SELECT * FROM vegetables').returning(null, [
    [ 'potatoes', '1kg' ],
    [ 'tomatoes', '500g' ]
]);

pgtest.connect('foo', function (err, client, done) {
    client.query('SELECT * FROM vegetables', function (err, data) {
        console.log(data);
        done();
    });
});

pgtest.check(); //No errors

Documentation

node-postgres-test is a drop in replacement for node-postgres, intended to be used in unit testing.

Before running your tests, you tell node-postgres-test what are the queries you are expecting and what should they return. After that, you will call pgtest.check(), to make sure everything was called in the order and with the parameters expected.

You will most likely want to use node-postgres-test with a tool like rewire to inject node-postgres-test to the module you are testing.

pgtest.connect(db, callback):

This is the mock function for pg.connect. It will call the callback with (null, client, done). Do your queries using client, when you finish using the client, done() must be called.

pgtest.expect(query, [params]).[returning(err, data)]:

Expect the that query to be sent to pgtest. The query can be a string or a regular expression. If it is a string, the query and the expected query must match exactly. If it is a regular expression, the query must match that expression.

Params is an optional parameter that contains the query's parameters, useful for testing queries with parameters.

These queries will return, by default, no error and no data (that is, the callback for the query will be called as cb(null, null).

With returning() you can customize the return value of the query. The first parameter is the error value, that will be sent directly to the callback. The second parameter is the data returned to the client, it has to be a row list, and it will be sent to the callback as {rows: data}.

pgtest.check()

Make sure all expected queries were run and that done() was properly called after finishing.

pgtest.reset()

Reset the module internal data structures, it is good practice to do that before every test, to make sure other tests do not interfere with the current one.

Comments, suggestions?

Yes, please! If you would like something to be added/fixed/etc, feel free to open an issue (or even better, a pull request :).