mohammad pakivand
mohammad pakivand

Reputation: 391

Javascript nodeJs Postgres pg unit test

I'm using PG library to connecting to Postgres DB,

Now, I want to write unit tests for database access and I don't know how I can do that. Actually, I need some mock Postgres server or something to mock Postgres.

I'm using Mocha for testing

below is one of my classes to access database

import { Pool } from "pg";
export class DbAccess implements IdbAccess {
  private static readonly postgres = new Pool();

  constructor(@inject(TYPES.ILogger) private readonly logger: ILogger) {}

  public saveConsumer(consumer: IAPIGWConsumer): Promise<IAPIGWConsumer> {
    return this.queryOne`
        INSERT INTO users (consumer_id, email)
        VALUES (${consumer.consumer_id}, ${consumer.email})
        ON CONFLICT (consumer_id) DO UPDATE SET email = ${consumer.email}
        RETURNING *
    `;
  }
}

I would appreciate any help, thank you.

Upvotes: 2

Views: 4406

Answers (1)

Lin Du
Lin Du

Reputation: 102267

If you want to mock or stub some package/method/module, you need to install a mock/stub library such as sinon.js, jest.js to achieve this.

Here is the unit test solution using sinon.js. For simplicity and clarity, I've removed unnecessary parts, such as DI.

db.ts:

import { Pool } from "pg";

export class DbAccess {
  private static readonly postgres = new Pool();

  public saveConsumer(consumer) {
    return DbAccess.postgres.query(`
        INSERT INTO users (consumer_id, email)
        VALUES (${consumer.consumer_id}, ${consumer.email})
        ON CONFLICT (consumer_id) DO UPDATE SET email = ${consumer.email}
        RETURNING *
    `);
  }
}

db.test.ts:

import pg from "pg";
import sinon from "sinon";
import { expect } from "chai";

describe("59624370", () => {
  afterEach(() => {
    sinon.restore();
  });
  it("should pass", async () => {
    const mPool = { query: sinon.stub().resolves({ rows: [] }) };
    const poolStub = sinon.stub(pg, "Pool").callsFake(() => mPool);
    const { DbAccess } = require("./db");
    const db = new DbAccess();
    const consumer = { consumer_id: 1, email: "[email protected]" };
    const actual = await db.saveConsumer(consumer);
    expect(actual).to.be.eql({ rows: [] });
    sinon.assert.calledOnce(poolStub);
    sinon.assert.calledOnce(mPool.query);
  });
});

Unit test results with coverage report:

 59624370
    ✓ should pass (132ms)


  1 passing (141ms)

------------|----------|----------|----------|----------|-------------------|
File        |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
------------|----------|----------|----------|----------|-------------------|
All files   |      100 |      100 |      100 |      100 |                   |
 db.test.ts |      100 |      100 |      100 |      100 |                   |
 db.ts      |      100 |      100 |      100 |      100 |                   |
------------|----------|----------|----------|----------|-------------------|

Source code: https://github.com/mrdulin/mocha-chai-sinon-codelab/tree/master/src/stackoverflow/59624370

Upvotes: 3

Related Questions