[英]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:我正在使用 Mocha 测试应用程序,当我执行“node helper_test.js”时,出现以下错误:
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. ReferenceError: before is not defined where before 是 mocha 钩子,"helper_test.js" 是一个测试文件,我想通过向用户介绍 mongoDB 数据库开始。
file: helper_test.js:文件: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.我认为问题在于我试图使用
node helper_test.js
命令启动它,实际上我必须使用 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: 1st 我们转到 package.json 并在脚本中进行测试并放置“mocha”,它看起来像这样:
"scripts": {
"test": "mocha"
},
Then we do npm test
and the test is launched.然后我们做
npm test
并启动测试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.