我找到了一些解决此问题的方法,但它们可能无法兑现承诺。我想在每次测试后清除数据库。我正在尝试从请求响应中保存id
并将其收集/传递给afterEach
。不幸的是,promise不会覆盖值,并且按定义为空。
describe('Should check DB setup', () => {
let value;
let request = chai.request('http://localhost:81')
.post('/api/report')
.send(mock);
let db = require(process.env.DB_DRIVER)(process.env.DB_NAME);
it('Checks do DB has table', () => {
request
.then((res) => {
let query = db.prepare('PRAGMA table_info('+process.env.ORDERS_TABLE+')').get();
db.close();
value = 'Win Later';
expect(query).is.not.a('undefined');
});
});
afterEach(() => {
console.log(value); //undefined
});
});
答案 0 :(得分:1)
您需要从测试中返回承诺,以便Mocha在完成测试之前等待它。
it('Checks do DB has table', () => {
return request
.then((res) => {
let query = db.prepare('PRAGMA table_info('+process.env.ORDERS_TABLE+')').get();
db.close();
value = 'Win Later';
expect(query).is.not.a('undefined');
});
});