Jest或Mocha:基于异步初始化动态创建测试 [英] Jest or Mocha: Dynamically create tests based on async initialization

查看:227
本文介绍了Jest或Mocha:基于异步初始化动态创建测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过循环从异步调用返回的数组来动态生成测试.我只是不知道如何做到这一点-使用摩卡咖啡或玩笑.为了说明如何使用代码,下面的同步示例起作用:

I'm trying to generate tests dynamically by looping over an array returned from an async call. I just cannot figure out how to do this - either using mocha or using jest. To illustrate using code, the following synchronous example works:

describe("Test using various frameworks", () => {
    ["mocha", "jest"].forEach(framework => {
        it(`Should test using ${framework}`, () => {
            expect(true).toBe(true);
        });
    });
});

但是,如果该数组是异步获取的,那么我无法让测试框架等到数组被获取后再尝试遍历它.

However, if that array is fetched asynchronously, I cannot get the testing frameworks to wait until the array is fetched before trying to loop over it.

async function getFrameworks() {
    //TODO: get it from some async source here
    return ["mocha", "jest"];
}

describe("Test using various frameworks", () => {
    var frameworks;
    //before() instead of beforeAll() if using mocha
    beforeAll(async ()=> {
        frameworks = await getFrameworks();
    });

    frameworks.forEach(framework => {
        it(`Should test using ${framework}`, () => {
            expect(true).toBe(true);
        });
    });
});

此操作失败,提示Cannot read property 'forEach' of undefined.我已经尝试过使用async/awaitPromise并进行done回调的所有组合,但无济于事.

This fails saying Cannot read property 'forEach' of undefined. I've tried all sort of combinations of using async/await and Promise and passing in a done callback but to no avail.

我最接近的是使用Mocha的 --delay 标志,但是仅解决部分问题.在实际用例中,我真正想做的是before()beforeAll()挂钩中运行一些异步初始化,然后使用它们动态生成测试.

The closest I came to this was using Mocha's --delay flag, but that only solves part of the problem. What I really want to do in my actual use case is to run some async intialization in the before() or beforeAll() hooks which I then use to dynamically generate tests.

是否有任何有关如何使用摩卡或开玩笑的指示?

Any pointers on how to do this using either mocha or jest?

推荐答案

要回答我自己的问题,我没有找到使用Jest或Mocha做到这一点的方法,但是我可以使用轻按.

To answer my own question, I did not find a way to do this using Jest or Mocha, but I could accomplish it using tap - which I used through babel-tap.

import tap from "babel-tap";

async function getFrameworks() {
    //TODO: get it from some async source here
    return ["mocha", "jest"];
}

getFrameworks().then(frameworks => {
    frameworks.forEach(framework => {
        tap.test(`Should test using ${framework}`, (tester) => {
            tester.ok("It works!");
        });
    }); 
});

不过,您可以做更多的事情.例如,您可以通过进一步调用tester.test()来创建嵌套作用域.另外,由于tap不具有beforeafter等的概念,(除非您使用

You can do a lot more though. You can create nested scopes by further calling tester.test() for example. Also, since tap does not have the concept of before, after etc, (unless you use the Mocha-like DSL ), you can simply use imperative code to simulate the equivalent behavior.

此外,您可以在测试中自由使用async/await样式调用.

Also, you can freely use async/await style calls inside tests.

这篇关于Jest或Mocha:基于异步初始化动态创建测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆