【问题标题】:Typescript and async initialisation outside protractor it量角器外部的打字稿和异步初始化
【发布时间】:2021-04-06 23:48:12
【问题描述】:

我从带有csv-parserCSV 文件中读取了一个测试配置,它的行为是异步的。当我将解析器包装在 Promise 中并在我的异步 it 测试用例中使用 await 时,配置数据被解析......在“它”之外它没有被解析,因为 describe 不支持 @987654324 @ 和 module: commonjs 不允许顶级 await。 配置数据包含用于在具有不同参数的循环中在“it”测试用例上运行的测试数据。 所以我需要一种方法:

  1. 解决“它”之外的承诺以获取配置数据或
  2. 找到一种方法来等待csv-parser stream/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


    【解决方案1】:

    将初始化函数放在 beforeAll/beforeEach 块中。然后矩阵在每个它中都可用

    describe('your test', () => {
      let matrix2;
    
      beforeAll(async () => {
        matrix2 = await initCountryMatrixFromCsv();
      });
    
      it('my test', () => {
        expect(matrix2).toBeTruthy(); // do more verifications ...
      });
    });
    

    还要确保resolve函数中的promise。我猜你希望它在 on('end') 中解决

    【讨论】:

    • 是的,我解决了 on('end') 中的承诺。我也已经尝试过 beforeAll/beforeEach 并且在 'it' 内部它已解决但不在'it' 外部。所以我不能在它之外使用 matrix.forEach。
    【解决方案2】:

    我假设您将 Protractor 与 Jasmine 一起使用(尽管这并不重要)。

    Jasmine 将在您的实际 initCountryMatrixFromCsv 方法解决之前尝试解决测试用例。 这背后的原因很简单,它需要知道有多少测试作为其设置的一部分。

    我在测试中遇到了同样的问题,解决方法是读取 CSV 文件同步。

    为此我使用了csv-load-sync npm 包,然后读取文件:

    import * as fs from 'fs';
    import * as loader from 'csv-load-sync';
    import * as path from 'path';
    
    readTestDataCsvSync() {
        const filePath = path.join(__dirname, 'TestData.csv');
        try {
          if (fs.existsSync(filePath)) {
            return loader(path.join(__dirname, 'TestData.csv'));
          }
        }
        catch (err) {
          throw new Error(`Couldn't load the test cases CSV file: ${err}`);
        }
      }
    

    现在您可以像以前一样进行测试:

    describe('E2E-I18N-CMX: test country matrix', () => {
        const matrix = readTestDataCsvSync(); 
        
        matrix.forEach((shopFunction, roleName) = > {
            it('Test ' + role, async (){
                // your test
            });
        });
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-01
      • 1970-01-01
      • 2023-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多