Registry / testing / node-mongodb-fixtures

node-mongodb-fixtures

JSON →
library3.2.9jsnpmunverified

A package and CLI for setting up and tearing down MongoDB test fixtures. Version 3.2.9 supports both JSON and JavaScript fixture files, custom collection scripts for indexes, and programmatic or CLI usage. Release cadence is irregular, maintained as of 2023. Key differentiators include first-class support for JavaScript fixtures (allowing ObjectId generation) and 'collection scripts' for injecting custom logic during load. Compared to similar tools (mongodb-memory-server, fixtures libraries), this package focuses purely on fixture loading/unloading with no in-memory server.

npm install node-mongodb-fixtures
INSTALL
IMPORT
SIG · NODE-MONGODB-FIXTU
N
node-mongodb-fixtures
testingjavascriptv3.2.9
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.

Fixtures
const Fixtures = require('node-mongodb-fixtures')
import Fixtures from 'node-mongodb-fixtures'
Package is CommonJS-only; no ES module default export. Use require() or await import() with default hack.
Fixtures (with options)
const Fixtures = require('node-mongodb-fixtures'); new Fixtures({ mute: true, dir: './fixtures' })
const { Fixtures } = require('node-mongodb-fixtures')
Fixtures is exported as a single constructor function, not a named export. Do not destructure.
programmatic usage
fixtures.connect('mongodb://localhost/test').then(() => fixtures.load())
fixtures.load({ uri: 'mongodb://localhost/test' })
connect() must be called before load(); options like uri are not passed to load().

Shows programmatic usage: create Fixtures instance, connect, unload existing data, load fixtures, then disconnect.

const Fixtures = require('node-mongodb-fixtures'); const fixtures = new Fixtures({ dir: './fixtures', mute: false }); fixtures .connect('mongodb://localhost:27017/test', { useNewUrlParser: true, useUnifiedTopology: true }) .then(() => fixtures.unload()) .then(() => fixtures.load()) .then(() => fixtures.disconnect()) .catch(err => console.error(err));
Debug
Known issues
breakingStarting in v3.0.0, the `Fixtures` constructor no longer accepts a `uri` option; you must call `connect()` separately.
fix
Use `new Fixtures({ dir: '...' })` (without uri) then `.connect('mongodb://...')`.
affects: >=3.0.0
deprecatedThe `useNewUrlParser` and `useUnifiedTopology` options passed to `connect()` are deprecated in the underlying MongoDB driver v5+. They are ignored (no effect) and should be removed.
fix
Remove `useNewUrlParser` and `useUnifiedTopology` options from connect(); MongoDB driver 5+ uses new behavior by default.
affects: >=3.2.0
gotchaIf you use `mute: true` in the constructor, all console output is suppressed, including error messages from the library.
fix
Set `mute: false` during development to see errors, or implement your own logging via `debug` module.
affects: >=3.0.0
gotchaCollection scripts (files ending with `_`) must not be detected as fixture data files; they are executed for side effects only.
fix
Name script files like `people_.js` (the collection name before underscore) and ensure they return a function that modifies the collection.
affects: >=3.0.0
breakingIn v2.x, the constructor accepted a `dir` argument; in v3.x it must be passed as an option object `{ dir: './fixtures' }`.
fix
Change `new Fixtures('./fixtures')` to `new Fixtures({ dir: './fixtures' })`.
affects: >=3.0.0
Errors
Common errors & fixes
TypeError: fixtures.connect is not a function
Attempted to destructure Fixtures as a named export or used invalid import syntax (e.g., ES module import without default).
fix
Use `const Fixtures = require('node-mongodb-fixtures');` then call `new Fixtures()`. For ES modules: `const { default: Fixtures } = await import('node-mongodb-fixtures');`
Error: Cannot find module './fixtures/people.json'
The `dir` option is not set correctly or the path is relative to cwd. Fixtures resolves relative to the current working directory, not the script file.
fix
Provide an absolute path or set `dir` relative to `process.cwd()`. For example: `new Fixtures({ dir: path.join(__dirname, 'fixtures') })`
MongoError: Authentication failed
Connection string does not include credentials or uses wrong auth mechanism (e.g., SCRAM-SHA-1 vs SCRAM-SHA-256).
fix
Include credentials in URI: `mongodb://user:pass@localhost:27017/test?authSource=admin`. Verify MongoDB server authentication settings.
TypeError: fixtures.load is not a function after .connect()
`.connect()` returns a Promise/Fixtures instance, but you may have chained incorrectly or used `.load()` before the Promise resolved.
fix
Chain `.then()` after `.connect()`: `fixtures.connect(...).then(() => fixtures.load())`.
Upgrade
Version history
3.2.9latest on npm
Audit
Dependencies
mongodbrequiredRequired for MongoDB client operations (loading/unloading connection)
commanderoptionalUsed for CLI command parsing
asyncrequiredUsed internally for async flow control
Agent activity
12 hits · last 30 days
node
10
Amazon
1
Resources