Jestjs 开玩笑地有条件地运行测试

Jestjs 开玩笑地有条件地运行测试,jestjs,performance-testing,Jestjs,Performance Testing,我一直在比较函数范式和面向对象范式 作为其中的一部分,我想做一些性能测试 我现在有一些类似的测试: it("Some long running performance test", () => { const result = myFunctionWithLotsOfData(); }); 现在,我只是打印这段代码运行的时间(大约5000毫秒) 我喜欢对它提供的所有断言和模拟功能使用Jest,以及它的实时重新加载等等 但是,我不希望这些测试一直

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

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

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

it("Some long running performance test", () => {
    const result = myFunctionWithLotsOfData();       
}); 
现在,我只是打印这段代码运行的时间(大约5000毫秒)

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

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


最好的方法是什么?

这里有一个解决方案,创建
itif
函数,这样我们就可以根据某些条件运行单元测试

例如,
itif
功能:

export const itif=(名称:字符串,条件:()=>boolean | Promise,cb)=>{
它(名称,异步完成=>{
if(等待条件()){
cb(完成);
}否则{
warn(`[跳过]:${name}`);
完成();
}
});
};
单元测试:

description('testsuites',()=>{
itif(
“功能方法-2性能测试”,
async()=>process.env.PERFORMANCE\u TEST===“true”,
完成=>{
控制台信息(“功能方法2性能测试”);
const t0=日期。现在();
const m0=getMemory();
const li0=实例化非递归(20,2,0,0,1,1,2,1);
const r0=getDrawablesFromLineInstances(li0);
printMemory(getMemory()-m0);
console.info(`Length:${r0.Length}`);
console.info(`Time-take:${Date.now()-t0}ms`);
完成();
}
);
});
process.env.PERFORMANCE\u test
环境变量的值等于
'true'
时,运行单元测试,结果如下:

PERFORMANCE\u TEST=true npm t--/Users/elsa/workspace/github.com/mrdulin/jest codelab/src/stackoverflow/58264344/index.spec.t
>开玩笑-codelab@1.0.0test/Users/elsa/workspace/github.com/mrdulin/jest-codelab
>jest--detectOpenHandles“/Users/elsa/workspace/github.com/mrdulin/jest-codelab/src/stackoverflow/58264344/index.spec.ts”
通过src/stackoverflow/58264344/index.spec.ts
测试套件
✓ 功能接近2性能测试(18ms)
console.info src/stackoverflow/58264344/index.spec.ts:22
功能方法2性能测试
console.log src/stackoverflow/58264344/index.spec.ts:4
0
console.info src/stackoverflow/58264344/index.spec.ts:28
长度:0
console.info src/stackoverflow/58264344/index.spec.ts:29
所用时间:5毫秒
测试套件:1个通过,共1个
测试:1项通过,共1项
快照:共0个
时间:5.67秒,估计9秒
未设置
process.env.PERFORMANCE\u test
环境变量的值时,请勿运行单元测试:

npm t--/Users/elsa/workspace/github.com/mrdulin/jest-codelab/src/stackoverflow/58264344/index.spec.ts
>开玩笑-codelab@1.0.0test/Users/elsa/workspace/github.com/mrdulin/jest-codelab
>jest--detectOpenHandles“/Users/elsa/workspace/github.com/mrdulin/jest-codelab/src/stackoverflow/58264344/index.spec.ts”
通过src/stackoverflow/58264344/index.spec.ts
测试套件
✓ 功能接近2性能测试(11ms)
console.warn src/stackoverflow/58264344/index.spec.ts:11
[跳过]:功能接近2性能测试
测试套件:1个通过,共1个
测试:1项通过,共1项
快照:共0个
时间:2.758s,估计5s
常量itif=(条件)=>条件?it:it.skip;
描述('套件名称',()=>{
itif(true)(“测试名称”,异步()=>{
//你的测试
});
});

与公认答案相同:

const maybe = process.env.JEST_ALLOW_INTEG ? describe : describe.skip;
maybe('Integration', () => {
  test('some integration test', async () => {
    expect(1).toEqual(1);
    return;
  });
});

这可能很有趣:如果我在描述中有2个或更多的It块,则上述解决方案不起作用。请让我知道,如果你有这个样品的解决方案。