【问题标题】:Restore between tests with Hapi Lab使用 Hapi Lab 在测试之间恢复
【发布时间】:2021-04-18 15:56:17
【问题描述】:

上下文:

我正在使用 Hapi Lab 运行一堆测试。

我还有一组额外的文件可以导出模拟 JSON 数据。

有什么问题:

每个测试都将 JSON 文件作为其例程的一部分进行操作。

第一个测试总是通过,因为它处理的是“干净”的 JSON 数据,而后面的测试有时会失败,因为之前的测试在之前的测试运行时修改了 JSON。

同样,如果我一次只运行一个文件的测试,输出就很好。然而,如果我尝试测试包含多个测试的目录,则会由于 JSON 格式错误而失败。

npm run lab /test-directory/test1.js    // Works fine
npm run lab /test-directory/test2.js    // Works fine

npm run lab /test-directory    //Fails

问题是什么:

是否有一种方便的方法让 Lab 在测试之间“恢复”原始状态,而不是让同一 JSON 数据实例在不同测试之间泄漏?

【问题讨论】:

    标签: node.js testing hapi


    【解决方案1】:

    我发现解决此问题的一个方便的解决方法是使用 node child processes. 在其自己的工作程序中运行每个测试

    我添加了一个文件run-tests-sequentially.js 依次调用测试:

    const { promisify } = require('util');
    const { exec } = require('child_process');
    
    const execAsync = promisify(exec);
    
    (async () => {
      console.log('Running test1...');
      const { stdout: test1Output } = await execAsync('npm run test:integration /test-directory/test1.js');
      console.log('Completed test1...');
      console.log(test1Output);
    
      console.log('Running test2...');
      const { stdout: test2Output } = await execAsync('npm run test:integration /test-directory/test2.js');
      console.log('Completed test2...');
      console.log(test2Output);
    })();
    

    然后我打电话给node ./run-tests-sequentially.js 运行序列。

    每个测试都在“干净”的工作人员中运行,没有数据泄漏。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-02
      • 2015-10-26
      • 1970-01-01
      • 2018-04-01
      • 2015-09-17
      • 2020-04-09
      相关资源
      最近更新 更多