Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

224
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda