【发布时间】:2021-04-06 23:48:12
【问题描述】:
我从带有csv-parser 的CSV 文件中读取了一个测试配置,它的行为是异步的。当我将解析器包装在 Promise 中并在我的异步 it 测试用例中使用 await 时,配置数据被解析......在“它”之外它没有被解析,因为 describe 不支持 @987654324 @ 和 module: commonjs 不允许顶级 await。
配置数据包含用于在具有不同参数的循环中在“it”测试用例上运行的测试数据。
所以我需要一种方法:
- 解决“它”之外的承诺以获取配置数据或
- 找到一种方法来等待
csv-parserstream/pipe/on 完成,然后再返回配置数据。
export function initCountryMatrixFromCsv() {
return new Promise <Map<string, ShopFunction>>((resolve, reject) => {
const countryShopFunctions = new Map<string, ShopFunction>();
countryShopFunctions.set(ALL_FUNCTIONS, new Map<string, ShopFunction>());
const fs = require('fs');
const csv = require('csv-parser');
const parsedCsv = [];
// behaves async... returns imediately and without the promise countryShopFunctions map is not filled:
fs.createReadStream(__dirname + '/country_matrix.csv')
.pipe(csv({ separator: ',' }))
.on('headers', async (headers) => {
// some header inits
}
)
.on('data', async (data) => await parsedCsv.push(data))
.on('end', async () => {
// init configuration in countryShopFunctions
});
});
describe('E2E-I18N-CMX: test country matrix', () => {
// a promise... await not alowed here
const matrix = initCountryMatrixFromCsv();
// not possible since matrix is a promise
matrix.forEach((shopFunction, roleName) = > {
it('Test ' + role, async (){
// perform test with shopFunction params
// first place to resolve the promise ... but i need it outside the it
const matrix2 = await initCountryMatrixFromCsv();
});
});
});
我尝试了几种带有和不带有 Promise 的变体,但当我不使用带有 await 的 Promise 时,所有变体都以空地图告终。
【问题讨论】:
标签: typescript promise async-await stream protractor