当我在mocha
函数中使用promise时,我的before
测试挂起。我下面的before
调用了一个返回诺言的外部函数。
describe('User Tests', () => {
let token = '';
before(() => {
const afunc = require('../../../controllers/somefunction');
return afunc().then((val) => {
console.log('what is val?', val);
});
});
//... other test are below it like...
describe('get own profile', () => {
it('should return profile of logged-in user', (done) => {
chai.request(app)
.get('/access/users/profile')
.set('authorization', `Bearer ${token}`)
.end((err, res) => {
expect(res.statusCode).to.equal(200);
done();
});
});
});
});
afunc
中的 ../../../controllers/somefunction
本身可以正常工作(在数据库断开连接后返回),如下所示:
require('dotenv').config();
const mongoose = require('mongoose');
const User = require('../../models/access/user');
// Use native promises
mongoose.Promise = global.Promise;
const db = mongoose.connect(`mongodb://localhost/${availableDBs[dbTarget] || availableDBs.test}`, {
keepAlive: true,
reconnectTries: 3,
useMongoClient: true,
});
// const db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
const dbconnect = () => new Promise((resolve) => {
db.once('open', () => {
console.log('DB Open and Listening');
resolve('DB Open and Listening');
});
});
const newSuperUser = async () => {
await dbconnect();
const user = await User.findOne({ username: data.username.toLowerCase() });
if (user === null) {
console.log('Create new user');
const newUser = await new User(data).save();
console.log('newUser', newUser);
if (newUser === null) {
return console.log('Username already exists.');
}
console.log('Successfully created new user.');
} else {
console.log('Deleting original user');
const deleted = await User.findOneAndRemove({ username: data.username });
console.log('Deleted user', deleted);
console.log('Create new user after deleting original');
const newUser = await new User(data).save();
console.log('newUser', newUser);
if (newUser === null) {
return console.log('Username already exists.');
}
console.log('Successfully created new user.');
}
db.close();
await new Promise((resolve) => {
db.on('disconnected', () => {
console.log('***************************************Mongoose CONNECTION TERMINATED');
resolve('user ready');
});
});
return true;
};
if (process.argv.some(array => ['test', 'pro', 'dev'].includes(array))) {
newSuperUser();
}
module.exports = newSuperUser;
console.log显示它返回一个值。我还尝试了以下替代方法并获得相同的结果:
describe('User Tests', () => {
let token = '';
before((done) => {
const afunc = require('../../../controllers/somefunction');
afunc().then((val) => {
console.log('what is val?', val);
done();
});
});
//... other test are below it like...
describe('get own profile', () => {
it('should return profile of logged-in user', (done) => {
chai.request(app)
.get('/access/users/profile')
.set('authorization', `Bearer ${token}`)
.end((err, res) => {
expect(res.statusCode).to.equal(200);
done();
});
});
});
});
我什至试图将before
包裹在另一个describe
中,但是(此时,它甚至不再运行before
):
describe('User Tests', () => {
let token = '';
describe('User Tests', () => {
before((done) => {
const afunc = require('../../../controllers/somefunction');
afunc().then((val) => {
console.log('what is val?', val);
done();
});
});
});
//... other test are below it like...
describe('get own profile', () => {
it('should return profile of logged-in user', (done) => {
chai.request(app)
.get('/access/users/profile')
.set('authorization', `Bearer ${token}`)
.end((err, res) => {
expect(res.statusCode).to.equal(200);
done();
});
});
});
});
我尝试返回诺言和调用done()
的备用方法。为什么摩卡咖啡会挂在这里?
Mocha 5.2.0