Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

232
Views
How to setup Unit test with mocha and mongodb-memory-server?

I am trying to make test case suite with mocha and mongodb-memory-server(as an in memory db). I am trying to implement this in the way below.

Project structure:

enter image description here

test runner: (in package.json)

"scripts": {
    "test": "mocha 'app/**/*.spec.js' --recursive --exit"
  },

So, First I need to initialise the in memory MongoDB, thats why I am using global.spec.js which looks like this,

const { MongoMemoryServer } = require("mongodb-memory-server");
const mongoose = require("mongoose");
mongoose.set("usePushEach", true);

let mongoServer;

before(async function () {
  // mongod donwload on first time
  this.timeout(30 * 1000);
  mongoServer = new MongoMemoryServer();
  const mongoUri = await mongoServer.getUri();
  await mongoose.connect(mongoUri, {});
  process.env.AGENDA_DB_URI = mongoUri;
});

after(function () {
  mongoose.disconnect();
  mongoServer.stop();
});

and a test-setup.js file which looks like this,

const { MongoMemoryServer } = require("mongodb-memory-server");

(async function() {
  // trigger downloading mongodb executable on first time
  const mongoServer = new MongoMemoryServer();
  await mongoServer.getUri();
  mongoServer.stop();
})()
  .then(() => {
    process.exit(0);
  })
  .catch(err => {
    console.error(err);
    process.exit(1);
  });

all *.spec.js files will be inside modules folder. In simple word each folder inside module will have one .spec.js file. If I try to run this using the npm run test command it is throwing me some error, that looks like this,

  1) "before all" hook

  0 passing (448ms)
  1 failing

  1) "before all" hook:
     Uncaught Error: TypeError: logWarnFn is not a function

I believe this logWarnFn error is coming from i18n. But when i start the server it is working fine.

versions:

"mongodb-memory-server": "^6.6.2",

"mocha": "^6.0.2",

"i18n": "0.8.3",
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!