3

When I run a jest test, creating a Pool instance when I require the pool, it returns a _pg.Pool is not a constructor error.

I have tried looking at the StackOverflow: pg.Pool is not a constructor

And this still does not work.

However, I am able to create a pool instance when I run the code, the error only shows up in Jest.

Node code:

import { Pool } from 'pg'

const pool = new Pool({configs})

export default pool

Error log:

● Test suite failed to run

    TypeError: _pg.Pool is not a constructor

    > 6 | const pool = new Pool({
        |              

      at Object.<anonymous> (src/resources/connection.js:6:14)
      at Object.require (src/routes/api.js:2:20)
      at Object.<anonymous> (src/__tests__/integration/user.test.js:8:1)

sidenote: the code is a copy of the documentation in https://node-postgres.com/api/pool

I don't expect an error to occur, since pg.Pool is a class with a constructor.

Lin Du
  • 88,126
  • 95
  • 281
  • 483
L. Ars
  • 49
  • 1
  • 7

2 Answers2

2

In case anyone comes across the same problem, I solved it by installing pg-pool, which has been merged into main pg package, and then importing pg-pool's Pool instead of pg's.

Reference: https://github.com/brianc/node-postgres/tree/master/packages/pg-pool

heniotierra
  • 108
  • 9
1

I wanted to add an alternate answer because for me the issue was very subtle and simple to fix.

I have a wrapper module that leverages pg and which historically has imported it via:

import * as postgresql from 'pg';

I've always used this.pool = new postgresql.Pool( { ...bits } );

This of course led to the following error today when updating my module to be pure ESM:

TypeError: postgresql.Pool is not a constructor
    at new PostgreSQLDriver (file:///home/rik/workspaces/kwaeri/node-kit/postgresql-database-driver/dist/src/postgresql-database-driver.mjs:35:278)
    at file:///home/rik/workspaces/kwaeri/node-kit/postgresql-database-driver/dist/test/postgresql-database-driver.mjs:23:559
    at ModuleJob.run (node:internal/modules/esm/module_job:193:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:528:24)
    at async importModuleDynamicallyWrapper (node:internal/vm/module:438:15)
    at async formattedImport (/home/rik/workspaces/kwaeri/node-kit/postgresql-database-driver/node_modules/mocha/lib/nodejs/esm-utils.js:7:14)
    at async exports.loadFilesAsync (/home/rik/workspaces/kwaeri/node-kit/postgresql-database-driver/node_modules/mocha/lib/nodejs/esm-utils.js:91:20)
    at async singleRun (/home/rik/workspaces/kwaeri/node-kit/postgresql-database-driver/node_modules/mocha/lib/cli/run-helpers.js:125:3)
    at async exports.handler (/home/rik/workspaces/kwaeri/node-kit/postgresql-database-driver/node_modules/mocha/lib/cli/run.js:370:5)

I tried using named exports as the documentation seems to suggest in the Check, use, return section, but that got me:

file:///home/rik/workspaces/kwaeri/node-kit/postgresql-database-driver/dist/src/postgresql-database-driver.mjs:23
cov_1u3o9s5ali=function(){return actualCoverage;};}return actualCoverage;}cov_1u3o9s5ali();import{Pool}from'pg';import{DatabaseDriver}from'@kwaeri/database-driver';// You'll need this line and the method definitions below
                                                                                                  ^^^^
SyntaxError: Named export 'Pool' not found. The requested module 'pg' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from 'pg';
const {return actualCoverage;};}return actualCoverage;}cov_1u3o9s5ali();import{Pool}from'pg';import{DatabaseDriver} = pkg;

... As I had actually expected it would.

Anyways, the fix is hinted to by typescript in the syntax error above - doh:

import Postgres from `pg`;

// ...
this.pool = new Postgres.Pool( { ...bits } );

That resolves the issue.

Rik
  • 676
  • 7
  • 11