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

164
Views
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 answers
Answer question

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 Report
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!