Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

223
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda