mongo-unit
Advanced tools
Comparing version 1.5.1 to 2.0.0
59
index.js
@@ -5,2 +5,3 @@ 'use strict' | ||
const MongoClient = require('mongodb').MongoClient | ||
const { MongoMemoryServer } = require('mongodb-memory-server') | ||
const fs = require('fs') | ||
@@ -19,17 +20,26 @@ const ps = require('ps-node') | ||
var mongodHelper | ||
var dbUrl = null | ||
var client | ||
var dbName | ||
let mongod = null | ||
let dbUrl = null | ||
let client | ||
let dbName | ||
function runMogo(opts, port) { | ||
const MongodHelper = require('mongodb-prebuilt').MongodHelper | ||
opts.port = port | ||
mongodHelper = new MongodHelper(['--port', port, '--dbpath', opts.dbpath, '--storageEngine', 'ephemeralForTest'], { | ||
version: opts.version, | ||
function runMongo(opts, port) { | ||
mongod = new MongoMemoryServer({ | ||
instance: { | ||
port: port, | ||
dbPath: opts.dbpath, | ||
dbName: opts.dbName, | ||
storageEngine: 'ephemeralForTest', | ||
}, | ||
binary: { | ||
version: opts.version, | ||
}, | ||
autoStart: false, | ||
}) | ||
return mongodHelper | ||
.run() | ||
return mongod | ||
.start() | ||
.then(() => { | ||
dbName = opts.dbName | ||
return mongod.getDbName() | ||
}) | ||
.then(dbName => { | ||
dbUrl = 'mongodb://localhost:' + port + '/' + dbName | ||
@@ -42,3 +52,5 @@ debug(`mongo is started on ${dbUrl}`) | ||
client = dbClient | ||
return dbUrl | ||
}) | ||
.catch(err => console.error(err)) | ||
} | ||
@@ -58,3 +70,3 @@ | ||
.then(() => getFreePort(mongo_opts.port)) | ||
.then(port => runMogo(mongo_opts, port)) | ||
.then(port => runMongo(mongo_opts, port)) | ||
} | ||
@@ -66,8 +78,12 @@ } | ||
} | ||
function stop() { | ||
return client.close(true).then(() => { | ||
mongodHelper && mongodHelper.mongoBin.childProcess.kill() | ||
dbUrl = null | ||
return delay(100) //this is small delay to make sure kill signal is sent | ||
}) | ||
return client | ||
.close(true) | ||
.then(() => mongod.stop()) | ||
.then(() => { | ||
// mongodHelper && mongodHelper.mongoBin.childProcess.kill() | ||
dbUrl = null | ||
return delay(100) //this is small delay to make sure kill signal is sent | ||
}) | ||
} | ||
@@ -146,3 +162,8 @@ | ||
if (process) { | ||
console.log('KILL PID: %s, COMMAND: %s, ARGUMENTS: %s', process.pid, process.command, process.arguments) | ||
console.log( | ||
'KILL PID: %s, COMMAND: %s, ARGUMENTS: %s', | ||
process.pid, | ||
process.command, | ||
process.arguments | ||
) | ||
ps.kill(process.pid) | ||
@@ -149,0 +170,0 @@ } |
{ | ||
"name": "mongo-unit", | ||
"version": "1.5.1", | ||
"version": "2.0.0", | ||
"description": "mongo db for unit tests", | ||
@@ -10,3 +10,5 @@ "main": "index.js", | ||
"test-example": "mocha ./examples/test.it.js --delay", | ||
"release": "git commit -am $npm_package_version && git tag $npm_package_version && git push && git push --tags && npm publish" | ||
"test-format": "prettier --check '**/*.{js,json}'", | ||
"release": "git commit -am $npm_package_version && git tag $npm_package_version && git push && git push --tags && npm publish", | ||
"format": "prettier --write '**/*.{js,json}'" | ||
}, | ||
@@ -26,3 +28,4 @@ "repository": { | ||
"mongodb", | ||
"mongoose" | ||
"mongoose", | ||
"mongodb-memory-server" | ||
], | ||
@@ -38,3 +41,3 @@ "author": "Mikhail Angelov", | ||
"mongodb": "^3.3.3", | ||
"mongodb-prebuilt": "~6.5.0", | ||
"mongodb-memory-server": "^6.3.1", | ||
"portfinder": "^1.0.10", | ||
@@ -46,5 +49,6 @@ "ps-node": "^0.1.4" | ||
"co": "^4.6.0", | ||
"mocha": "^3.2.0", | ||
"mongoose": "^5.7.7" | ||
"mocha": "^7.1.0", | ||
"mongoose": "^5.7.7", | ||
"prettier": "^1.19.1" | ||
} | ||
} |
# mongo-unit | ||
This library is done to simplify creation of integration tests for node.js application with Mongo DB. | ||
I starts local mongodb process using [mongodb-prebuilt](https://github.com/winfinit/mongodb-prebuilt) library, | ||
I starts local mongodb process using [mongodb-memory-server](https://github.com/nodkz/mongodb-memory-server) library, | ||
and it work in "InMemory" mode, which improve performance of your tests. | ||
@@ -28,5 +28,5 @@ | ||
mongoUnit.start().then(url => { | ||
console.log('fake mongo is started: ', url) | ||
process.env.DATABASE_URL = url // this var process.env.DATABASE_URL = will keep link to fake mongo | ||
mongoUnit.start().then(() => { | ||
console.log('fake mongo is started: ', mongoUnit.getUrl()) | ||
process.env.DATABASE_URL = mongoUnit.getUrl() // this var process.env.DATABASE_URL = will keep link to fake mongo | ||
run() // this line start mocha tests | ||
@@ -129,3 +129,3 @@ }) | ||
## Requirements | ||
It works on Node.js 8+ | ||
It works on Node.js 10+ | ||
@@ -139,8 +139,7 @@ ## Installation | ||
It starts mongod on one of available port and returns Promise with URL to connect to this db | ||
`opts` is optional params, you can specify some command line params for mongod | ||
(more about it in documentation for [mongodb-prebuilt](https://github.com/winfinit/mongodb-prebuilt)) | ||
`opts` is optional params, you can specify some command line params for mongod | ||
`opts.port` - preferable mongo db port, default: `27017` | ||
`opts.dbName` - name of test db, default: `test` | ||
`opts.dbpath` - db path, default: `<node_modules/mongo-unit>\.mongo-unit` | ||
`opts.verbose` - enable debug informaton for mongodb-prebuilt, default: `false` | ||
`opts.verbose` - enable debug logs, default: `false` | ||
@@ -147,0 +146,0 @@ ### `stop()` |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Native code
Supply chain riskContains native code (e.g., compiled binaries or shared libraries). Including native code can obscure malicious behavior.
Found 2 instances in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
171732
35
205
5
178
26
+ Addedmongodb-memory-server@^6.3.1
+ Added@types/tmp@0.2.6(transitive)
+ Addedagent-base@6.0.2(transitive)
+ Addedasync-mutex@0.3.2(transitive)
+ Addedbl@4.1.0(transitive)
+ Addedcamelcase@6.3.0(transitive)
+ Addedcommondir@1.0.1(transitive)
+ Addeddebug@4.4.0(transitive)
+ Addedfind-cache-dir@3.3.2(transitive)
+ Addedfind-package-json@1.2.0(transitive)
+ Addedfind-up@4.1.0(transitive)
+ Addedget-port@5.1.1(transitive)
+ Addedhttps-proxy-agent@5.0.1(transitive)
+ Addedlocate-path@5.0.0(transitive)
+ Addedmake-dir@3.1.0(transitive)
+ Addedmd5-file@5.0.0(transitive)
+ Addedmkdirp@1.0.4(transitive)
+ Addedmongodb-memory-server@6.10.0(transitive)
+ Addedmongodb-memory-server-core@6.10.0(transitive)
+ Addedp-limit@2.3.0(transitive)
+ Addedp-locate@4.1.0(transitive)
+ Addedp-try@2.2.0(transitive)
+ Addedpath-exists@4.0.0(transitive)
+ Addedpkg-dir@4.2.0(transitive)
+ Addedreadable-stream@3.6.2(transitive)
+ Addedsemver@6.3.17.6.3(transitive)
+ Addedtar-stream@2.2.0(transitive)
+ Addedtmp@0.2.3(transitive)
+ Addedtslib@2.8.1(transitive)
+ Addeduuid@8.3.2(transitive)
- Removedmongodb-prebuilt@~6.5.0
- Removedajv@6.12.6(transitive)
- Removedansi-regex@2.1.1(transitive)
- Removedasn1@0.2.6(transitive)
- Removedassert-plus@1.0.0(transitive)
- Removedasync@2.1.4(transitive)
- Removedasynckit@0.4.0(transitive)
- Removedaws-sign2@0.7.0(transitive)
- Removedaws4@1.13.2(transitive)
- Removedbalanced-match@1.0.2(transitive)
- Removedbcrypt-pbkdf@1.0.2(transitive)
- Removedbl@1.2.3(transitive)
- Removedbluebird@3.7.2(transitive)
- Removedbrace-expansion@1.1.11(transitive)
- Removedbuffer-alloc@1.2.0(transitive)
- Removedbuffer-alloc-unsafe@1.1.0(transitive)
- Removedbuffer-fill@1.0.0(transitive)
- Removedbuffer-from@1.1.2(transitive)
- Removedcamelcase@2.1.1(transitive)
- Removedcaseless@0.12.0(transitive)
- Removedcliui@3.2.0(transitive)
- Removedcode-point-at@1.1.0(transitive)
- Removedcombined-stream@1.0.8(transitive)
- Removedcommander@2.20.3(transitive)
- Removedconcat-map@0.0.1(transitive)
- Removedconcat-stream@1.6.2(transitive)
- Removedcore-util-is@1.0.2(transitive)
- Removeddashdash@1.14.1(transitive)
- Removeddebug@2.6.9(transitive)
- Removeddecamelize@1.2.0(transitive)
- Removeddecompress@4.2.1(transitive)
- Removeddecompress-tar@4.1.1(transitive)
- Removeddecompress-tarbz2@4.1.1(transitive)
- Removeddecompress-targz@4.1.1(transitive)
- Removeddecompress-unzip@4.0.1(transitive)
- Removeddelayed-stream@1.0.0(transitive)
- Removedecc-jsbn@0.1.2(transitive)
- Removedextend@3.0.2(transitive)
- Removedextsprintf@1.3.0(transitive)
- Removedfast-deep-equal@3.1.3(transitive)
- Removedfast-json-stable-stringify@2.1.0(transitive)
- Removedfile-type@3.9.05.2.06.2.0(transitive)
- Removedforever-agent@0.6.1(transitive)
- Removedform-data@2.3.3(transitive)
- Removedfs-extra@2.1.2(transitive)
- Removedfs.realpath@1.0.0(transitive)
- Removedget-stream@2.3.1(transitive)
- Removedgetos@2.8.4(transitive)
- Removedgetpass@0.1.7(transitive)
- Removedglob@7.2.3(transitive)
- Removedgraceful-fs@4.2.11(transitive)
- Removedhar-schema@2.0.0(transitive)
- Removedhar-validator@5.1.5(transitive)
- Removedhttp-signature@1.2.0(transitive)
- Removedinflight@1.0.6(transitive)
- Removedinvert-kv@1.0.0(transitive)
- Removedis-fullwidth-code-point@1.0.0(transitive)
- Removedis-natural-number@4.0.1(transitive)
- Removedis-stream@1.1.0(transitive)
- Removedis-typedarray@1.0.0(transitive)
- Removedisstream@0.1.2(transitive)
- Removedjsbn@0.1.1(transitive)
- Removedjson-schema@0.4.0(transitive)
- Removedjson-schema-traverse@0.4.1(transitive)
- Removedjson-stringify-safe@5.0.1(transitive)
- Removedjsonfile@2.4.0(transitive)
- Removedjsprim@1.4.2(transitive)
- Removedlcid@1.0.0(transitive)
- Removedmake-dir@1.3.0(transitive)
- Removedmd5-file@3.1.1(transitive)
- Removedmime-db@1.52.0(transitive)
- Removedmime-types@2.1.35(transitive)
- Removedminimatch@3.1.2(transitive)
- Removedmongodb-download@2.2.7(transitive)
- Removedmongodb-prebuilt@6.5.0(transitive)
- Removedms@2.0.0(transitive)
- Removednumber-is-nan@1.0.1(transitive)
- Removedoauth-sign@0.9.0(transitive)
- Removedobject-assign@4.1.1(transitive)
- Removedos-locale@1.4.0(transitive)
- Removedos-shim@0.1.3(transitive)
- Removedpath-is-absolute@1.0.1(transitive)
- Removedperformance-now@2.1.0(transitive)
- Removedpify@2.3.03.0.0(transitive)
- Removedpinkie@2.0.4(transitive)
- Removedpinkie-promise@2.0.1(transitive)
- Removedpsl@1.15.0(transitive)
- Removedpunycode@2.3.1(transitive)
- Removedqs@6.5.3(transitive)
- Removedrequest@2.88.2(transitive)
- Removedrequest-promise@4.2.6(transitive)
- Removedrequest-promise-core@1.1.4(transitive)
- Removedsafer-buffer@2.1.2(transitive)
- Removedseek-bzip@1.0.6(transitive)
- Removedsemver@5.7.2(transitive)
- Removedspawn-sync@1.0.15(transitive)
- Removedsshpk@1.18.0(transitive)
- Removedstealthy-require@1.1.1(transitive)
- Removedstring-width@1.0.2(transitive)
- Removedstrip-ansi@3.0.1(transitive)
- Removedstrip-dirs@2.1.0(transitive)
- Removedtar-stream@1.6.2(transitive)
- Removedthrough@2.3.8(transitive)
- Removedto-buffer@1.1.1(transitive)
- Removedtough-cookie@2.5.0(transitive)
- Removedtunnel-agent@0.6.0(transitive)
- Removedtweetnacl@0.14.5(transitive)
- Removedtypedarray@0.0.6(transitive)
- Removedunbzip2-stream@1.4.3(transitive)
- Removeduri-js@4.4.1(transitive)
- Removeduuid@3.4.0(transitive)
- Removedverror@1.10.0(transitive)
- Removedwindow-size@0.1.4(transitive)
- Removedwrap-ansi@2.1.0(transitive)
- Removedxtend@4.0.2(transitive)
- Removedy18n@3.2.2(transitive)
- Removedyargs@3.32.0(transitive)