Security News
New Python Packaging Proposal Aims to Solve Phantom Dependency Problem with SBOMs
PEP 770 proposes adding SBOM support to Python packages to improve transparency and catch hidden non-Python dependencies that security tools often miss.
mongo-unit
Advanced tools
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 library, and it work in "InMemory" mode, which improve performance of your tests.
Since running embedded mongo takes some time, we need to config mocha
to wait until mongo is up and running, the easiest way to do it add --delay
flag for mocha
and handle async flow, like this
//package.json
...
"scripts": {
...
"test": "mocha ./**/*.spec.js --delay",
...
},
...
add init.spec.js
//init.spec.js
const mongoUnit = require('mongo-unit')
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
run() // this line start mocha tests
})
after(() => {
const dao = require('./dao')
console.log('stop')
dao.close()
return mongoUnit.stop()
})
Code under test
//dao.js
const mongoose = require('mongoose')
const userSchema = new mongoose.Schema({
name: String,
})
const taskSchema = new mongoose.Schema({
userId: String,
task: String,
})
module.exports = {
init: () => mongoose.connect(process.env.DATABASE_URL),
close: () => mongoose.disconnect(),
User: mongoose.model('user', userSchema),
Task: mongoose.model('task', taskSchema),
}
Test data
{
"users": [
{
"_id": "56d9bf92f9be48771d6fe5b1",
"name": "test"
},
{
"_id": "56d9bf92f9be48771d6fe5b2",
"name": "John"
}
],
"tasks": [
{
"userId": "56d9bf92f9be48771d6fe5b1",
"task": "do stuff"
},
{
"userId": "56d9bf92f9be48771d6fe5b1",
"task": "fix stuff"
}
]
}
Mocha test
//dao.spec.js
const {expect} = require('chai')
const mongoUnit = require('mongo-unit')
const testData = require('./data.json')
const daoUT = require('./dao')
describe('dao', ()=>{
before(() => daoUT.init())
beforeEach(() => mongoUnit.load(testData))
afterEach(() => mongoUnit.drop())
it('should find all users', () => {
return daoUT.User.find()
.then(users => {
expect(users.length).to.equal(2)
expect(users[0].name).to.equal('test')
})
})
it('should find all tasks for user 1', () => {
return daoUT.User.find()
.then(users => users[0])
.then(user=>daoUT.Task.find({userId: user._id}))
.then(tasks => {
expect(tasks.length).to.equal(2)
expect(tasks[0].task).to.equal('do stuff')
})
})
})
I was inspired by dbUnit library, which is very popular in java world.
There is alternative library for mocking Mongo: mockgoose
It works on Node.js 8+
npm install -D mongo-unit
start(opts)
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)
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
stop()
It stops mongod process
getUrl()
Syncronius API returns URL to connect to test db, if test DB is not started it thows an Exception
load(data)
Inserts given data (like below) DB collections, returns Promise.
{
"collectionName1":[
{"field1":"value1"},
{"field2":"value2"}
],
"collectionName2":[
{"field3":"value3"},
{"field4":"value4"}
]
}
clean(data)
Clear collections based on given data (data format is the same), returns Promise.
drop()
Drops test DB, returns Promise.
initDb(url, data)
helper function, load db data into mongo (url)
dropDb(url)
helper function, clear all db data from mongo (url)
FAQs
mongo db for unit tests
The npm package mongo-unit receives a total of 885 weekly downloads. As such, mongo-unit popularity was classified as not popular.
We found that mongo-unit demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PEP 770 proposes adding SBOM support to Python packages to improve transparency and catch hidden non-Python dependencies that security tools often miss.
Security News
Socket CEO Feross Aboukhadijeh discusses open source security challenges, including zero-day attacks and supply chain risks, on the Cyber Security Council podcast.
Security News
Research
Socket researchers uncover how threat actors weaponize Out-of-Band Application Security Testing (OAST) techniques across the npm, PyPI, and RubyGems ecosystems to exfiltrate sensitive data.