在 Jest 中有条件地运行测试 [英] Conditionally run tests in Jest

查看:31
本文介绍了在 Jest 中有条件地运行测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在比较函数范式和面向对象范式.

I've been playing around comparing the functional paradigm and the object-oriented paradigm.

作为其中的一部分 - 我想做一些性能测试.

As part of this - I'm wanting to do some performance tests.

我现在有一些类似这样的测试:

I have some tests that look like this for now:

it("Some long running performance test", () => {
    const result = myFunctionWithLotsOfData();       
}); 

现在,我只是打印这段代码运行所需的时间(大约 5000 毫秒).

For now, I'm just printing how long this code takes to run (around 5000ms).

我喜欢将 Jest 用于它提供的所有断言和模拟功能,以及实时重新加载等.

I like using Jest for all of the assertions and mocking functionality it gives, and it's live reload, etc.

但是,我不希望一直进行这些测试,我会运行创建一个 npm 脚本,例如 npm test:performance,并且仅在存在环境变量或存在环境变量时才运行这些测试类似.

However, I don't want these tests to all the time, I'd run create a npm script like npm test:performance, and only run these tests if an environment variable is present or similar.

最好的方法是什么?

推荐答案

const itif = (condition) => condition ? it : it.skip;

describe('suite name', () => {
  itif(true)('test name', async () => {
    // Your test
  });
});

这篇关于在 Jest 中有条件地运行测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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