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

165
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
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