javascript - 如何在 Mocha 中将数据从 promise 传递到 afterEach?
问题描述
我为这个问题找到了一些回应,但它们可能不适用于承诺。我想在每次测试后清除数据库。我正在尝试id
从请求响应中保存并将其收集/传递给afterEach
. 不幸的是,promise 不会覆盖 value 并且它定义为 null。
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
});
});
解决方案
您需要从测试中返回承诺,以便 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');
});
});
推荐阅读
- c++ - UI 自动化 GetFocusedElement 返回根元素
- php - 如何循环 REST API 响应?
- azure - azure sdk 中的 ListByResourceGroup() 和 GetByResourceGroup() 有什么区别?
- php - 可以为最后一个单词和数字组合正则表达式吗?
- javascript - 使用 on resize 设置 div 高度?
- c# - 使用Lambda表达式查询SQL语句填充的DataTable
- python - Python - 列表:如何从具有嵌套属性的字典列表中删除空属性?
- python - 发送折叠行的 html 报告
- jquery - 如何获取已检查的表行。带分页
- r - 如果满足多个条件,则添加重复行