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();
});
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.