Registry / testing / mocha-docker-postgres

mocha-docker-postgres

JSON →
library1.0.1jsnpmunverified

Mocha test helper that automatically manages a Docker-based PostgreSQL container for integration tests. Version 1.0.1 is the latest stable release. It patches Mocha's Context prototype to provide `this.dockerPostgres()` and `this.conString`. The container is reused across tests (only started once per run), and each call creates a fresh database, enabling fast, isolated test databases without manual setup or teardown. Lighter than full testcontainers solutions; designed specifically for Mocha.

npm install mocha-docker-postgres
INSTALL
IMPORT
SIG · MOCHA-DOCKER-POSTG
M
mocha-docker-postgres
testingjavascriptv1.0.1
harness data pending
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.

mocha-docker-postgres
require('mocha-docker-postgres')
import 'mocha-docker-postgres'
Package is CJS-only; patches global Mocha Context, no exported symbols. Do not use ESM import.
this.dockerPostgres
before(function() { return this.dockerPostgres(); })
this.dockerPostgres() in arrow function (loses Mocha context)
Must use regular function (not arrow) to access Mocha context bound to `this`.
this.conString
const conString = this.conString
this.conString() as function
Property set after dockerPostgres completes; not a method.

Sets up a Docker PostgreSQL container before tests, provides connection string, and uses it to instantiate a module.

const mochaDockerPostgres = require('mocha-docker-postgres'); const someModule = require('../someModule'); describe('integration test', function () { let instance; before(function () { this.timeout(30000); return this.dockerPostgres().then(() => { instance = someModule({ conString: this.conString }); }); }); it('should query data', function () { return instance.query('SELECT 1').then(result => { // assert result }); }); });
Debug
Known issues
gotchaRequires Docker daemon to be running on the test machine.
fix
Ensure Docker is installed and running before executing tests.
affects: *
gotchaPatches Mocha Context globally - affects all tests in the suite, even those not needing Docker.
fix
Use a separate test file or conditional requires to isolate Docker-dependent tests.
affects: *
breakingArrow functions lose Mocha context; `this.dockerPostgres()` fails if called inside arrow function.
fix
Use `function()` keyword for hooks (before, beforeEach, etc.)
affects: *
Errors
Common errors & fixes
Error: Cannot find module 'dockerode'
Missing peer dependency dockerode
fix
Run `npm install --save-dev dockerode`
TypeError: this.dockerPostgres is not a function
Using arrow function as Mocha hook (loses context) or forgot to require the package
fix
Use `function()` keyword for before/beforeEach hooks; ensure require('mocha-docker-postgres') is called first.
Upgrade
Version history
1.0.1latest on npm
Audit
Dependencies
dockeroderequiredinteract with Docker daemon to manage containers
Agent activity
11 hits · last 30 days
node
8
Resources
mocha-docker-postgres — npm install mocha-docker-postgres · libregistry