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

166
Visualizações
Hook before problems using Mocha to perform backend tests

I am testing an application with Mocha and when I do "node helper_test.js" I get the following error:

How can I fix this error?

ReferenceError: before is not defined Where before is a mocha hook and "helper_test.js" is a test file where I want to start by introducing a user to a mongoDB database.

file: helper_test.js:

const mongoose = require('mongoose');
mongoose.Promise = global.Promise;
before(done => {
  mongoose.connect('mongodb://localhost:27017/test_app', {
    useNewUrlParser: true,
    useCreateIndex: true,
    useFindAndModify: false
  });
  mongoose.connection
    .once('open', () => {

      const user1 = {
        _id: '6181608b936f234576a24d4d',
        role: 'FREEUSER_ROLE',
        status: true,
        active: false,
        lang: "ESP",
        username: "BaldanHero",
        email: "baldanhero@gmail.com",
        password: "12345678",
      }

      user1.save();

       done();
    })
    .on('error', error => {
      console.log('Error', error);
    });
});

after(async () => {
  try {
    await mongoose.connection.dropDatabase();
  } catch (error) {
    console.log(error);
  }
  mongoose.connection.close();
});
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I think the problem is that I am trying to launch it using node helper_test.js command and actually I have to launch it using mocha. 1st We go to the package.json and in scripts we go to test and we put "mocha", in such a way that it would look like this:

"scripts": {
     "test": "mocha"
   },

Then we do npm test and the test is launched.

about 4 years ago · Juan Pablo Isaza Relatório
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