在Jest中有条件地运行测试

时间:2019-10-07 06:19:13

标签: jestjs performance-testing

我一直在比较功能范例和面向对象范例。

为此,我想做一些性能测试。

我现在有一些看起来像这样的测试:

it("functional-approach-2 perforance test", () => {
    console.info("Functional Approach 2 Performance Test"); 
    const t0 = Date.now(); 
    const m0 = getMemory(); 
    const li0 = instantiateFanRecursive(20, 2, 0, 0, 1, 1, 2, 1);
    const r0 = getDrawablesFromLineInstances(li0); 
    printMemory(getMemory() - m0); 
    console.info(`Length: ${r0.length}`); 
    console.info(`Time Taken: ${Date.now() - t0}ms`); 
}); 

就目前而言,我只是在打印此代码运行多长时间(大约5000毫秒)。

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

但是,我一直都不需要这些测试,我会运行创建npm test:performance之类的npm脚本,并且仅在存在或类似环境变量的情况下运行这些测试。

执行此操作的最佳方法是什么?

3 个答案:

答案 0 :(得分:10)

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

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

答案 1 :(得分:4)

与接受的答案相同:

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

答案 2 :(得分:0)

这里是一种解决方案,创建model { for (i in 1:N) { # N=number of datapoints in dataset #likelihood r[i] ~dbin(p[i],n[i]) p[i]<-1- exp(-h[i]*dt[i]) # hazard h over interval [t,t+dt] expressed as deaths per unit person-time (e.g. months) #fixed effects model log(h[i]) <- nu[i]+log(time[i])*theta[i] nu[i]<-mu[s[i],1]+d[s[i],1]*(1-equals(t[i],b[i])) theta[i]<-mu[s[i],2]+ d[s[i],2]*(1-equals(t[i],b[i])) } # priors d[1,1]<- 0 d[1,2]<- 0 for(j in 2 :NT){ # NT=number of treatments d[j,1:2] ~ dmnorm(mean[1:2],prec2[,]) } for(k in 1:NS) { mu[k,1:2] ~ dmnorm(mean[1:2],prec2[,]) } } #Winbugs data set list(N=176, NS=5, NT=3, mean=c(0,0), prec2 = structure(.Data = c(0.0001,0,0,0.0001), .Dim = c(2,2))) # initials 1 list( d=structure(.Data=c(NA,NA,0,0,0,0,0,0), .Dim = c(4,2)), mu = structure(.Data=c(1,1,1,1,1,1,1,1), .Dim = c(4,2))) # initials 2 list( d=structure(.Data=c(NA,NA,0.5,0.5,0.5,0.5,0.5,0.5), .Dim = c(4,2)), mu = structure(.Data=c(0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5), .Dim = c(4,2))) s[] r[] n[] t[] b[] time[] dt[] 1 1 62 1 1 3 2 1 2 59 1 1 7 4 1 6 53 1 1 11 2 1 2 51 1 1 13 2 1 3 48 1 1 15 2 1 2 45 1 1 17 2 1 5 40 1 1 19 2 1 2 37 1 1 23 4 1 2 35 1 1 25 2 1 2 32 1 1 27 2 1 1 31 1 1 29 2 1 2 28 1 1 31 2 1 2 26 1 1 33 2 1 2 23 1 1 35 2 1 1 21 1 1 39 4 1 1 14 1 1 51 12 1 2 55 2 1 5 4 1 1 54 2 1 7 2 1 2 52 2 1 9 2 1 1 51 2 1 11 2 1 5 46 2 1 13 2 1 2 44 2 1 15 2 1 3 41 2 1 17 2 1 3 37 2 1 19 2 1 2 35 2 1 21 2 1 1 34 2 1 23 2 1 1 33 2 1 25 2 1 1 32 2 1 31 6 1 3 29 2 1 33 2 1 1 28 2 1 35 2 1 1 26 2 1 39 4 1 1 24 2 1 41 2 1 1 22 2 1 43 2 1 2 19 2 1 45 2 2 8 169 1 1 3 4 2 10 148 1 1 5 2 2 8 137 1 1 7 2 2 6 127 1 1 9 2 2 8 118 1 1 11 2 2 7 109 1 1 13 2 2 3 105 1 1 15 2 2 4 95 1 1 17 2 2 3 84 1 1 19 2 2 3 76 1 1 21 2 2 4 68 1 1 23 2 2 4 60 1 1 25 2 2 4 50 1 1 27 2 2 1 35 1 1 31 4 2 2 29 1 1 33 2 2 1 25 1 1 35 2 2 3 21 1 1 37 2 2 1 18 1 1 39 2 2 2 11 1 1 43 4 2 1 180 2 1 1 2 2 11 162 2 1 3 2 2 9 147 2 1 5 2 2 9 135 2 1 7 2 2 6 125 2 1 9 2 2 6 116 2 1 11 2 2 6 106 2 1 13 2 2 7 95 2 1 15 2 2 1 92 2 1 17 2 2 5 84 2 1 19 2 2 3 77 2 1 21 2 2 2 67 2 1 23 2 2 1 59 2 1 25 2 2 4 49 2 1 27 2 2 1 40 2 1 29 2 2 2 34 2 1 31 2 2 3 23 2 1 37 6 2 1 19 2 1 39 2 4 1 62 1 1 3 2 4 2 59 1 1 7 4 4 6 53 1 1 11 2 4 2 51 1 1 13 2 4 3 48 1 1 15 2 4 2 45 1 1 17 2 4 5 40 1 1 19 2 4 2 37 1 1 23 4 4 2 35 1 1 25 2 4 2 32 1 1 27 2 4 1 31 1 1 29 2 4 2 28 1 1 31 2 4 2 26 1 1 33 2 4 2 23 1 1 35 2 4 1 21 1 1 39 4 4 1 14 1 1 51 12 4 2 55 2 1 5 4 4 1 54 2 1 7 2 4 2 52 2 1 9 2 4 1 51 2 1 11 2 4 5 46 2 1 13 2 4 2 44 2 1 15 2 4 3 41 2 1 17 2 4 3 37 2 1 19 2 4 2 35 2 1 21 2 4 1 34 2 1 23 2 4 1 33 2 1 25 2 4 1 32 2 1 31 6 4 3 29 2 1 33 2 4 1 28 2 1 35 2 4 1 26 2 1 39 4 4 1 24 2 1 41 2 4 1 22 2 1 43 2 4 2 19 2 1 45 2 5 8 169 1 1 3 4 5 10 148 1 1 5 2 5 8 137 1 1 7 2 5 6 127 1 1 9 2 5 8 118 1 1 11 2 5 7 109 1 1 13 2 5 3 105 1 1 15 2 5 4 95 1 1 17 2 5 3 84 1 1 19 2 5 3 76 1 1 21 2 5 4 68 1 1 23 2 5 4 60 1 1 25 2 5 4 50 1 1 27 2 5 1 35 1 1 31 4 5 2 29 1 1 33 2 5 1 25 1 1 35 2 5 3 21 1 1 37 2 5 1 18 1 1 39 2 5 2 11 1 1 43 4 5 1 180 2 1 1 2 5 11 162 2 1 3 2 5 9 147 2 1 5 2 5 9 135 2 1 7 2 5 6 125 2 1 9 2 5 6 116 2 1 11 2 5 6 106 2 1 13 2 5 7 95 2 1 15 2 5 1 92 2 1 17 2 5 5 84 2 1 19 2 5 3 77 2 1 21 2 5 2 67 2 1 23 2 5 1 59 2 1 25 2 5 4 49 2 1 27 2 5 1 40 2 1 29 2 5 2 34 2 1 31 2 5 3 23 2 1 37 6 5 1 19 2 1 39 2 3 2 179 1 1 1 2 3 4 172 1 1 3 2 3 3 168 1 1 5 2 3 6 157 1 1 7 2 3 4 151 1 1 9 2 3 9 142 1 1 11 2 3 10 130 1 1 13 2 3 7 123 1 1 15 2 3 3 119 1 1 17 2 3 5 112 1 1 19 2 3 3 108 1 1 21 2 3 3 103 1 1 23 2 3 12 91 1 1 25 2 3 2 68 1 1 27 2 3 2 46 1 1 29 2 3 8 29 1 1 31 2 3 2 23 1 1 33 2 3 3 8 1 1 35 2 3 5 175 3 1 3 4 3 7 163 3 1 5 2 3 12 151 3 1 7 2 3 12 139 3 1 9 2 3 4 132 3 1 11 2 3 9 122 3 1 13 2 3 7 114 3 1 15 2 3 4 108 3 1 17 2 3 7 101 3 1 19 2 3 5 96 3 1 21 2 3 7 89 3 1 23 2 3 2 87 3 1 25 2 3 4 68 3 1 27 2 3 4 50 3 1 29 2 3 3 40 3 1 31 2 3 3 22 3 1 33 2 3 1 8 3 1 35 2 END 函数,以便我们可以在某些条件下运行单元测试。

例如,itif函数:

itif

单元测试:

export const itif = (name: string, condition: () => boolean | Promise<boolean>, cb) => {
  it(name, async done => {
    if (await condition()) {
      cb(done);
    } else {
      console.warn(`[skipped]: ${name}`);
      done();
    }
  });
};

describe('test suites', () => { itif( 'functional-approach-2 perforance test', async () => process.env.PERFORMANCE_TEST === 'true', done => { console.info('Functional Approach 2 Performance Test'); const t0 = Date.now(); const m0 = getMemory(); const li0 = instantiateFanRecursive(20, 2, 0, 0, 1, 1, 2, 1); const r0 = getDrawablesFromLineInstances(li0); printMemory(getMemory() - m0); console.info(`Length: ${r0.length}`); console.info(`Time Taken: ${Date.now() - t0}ms`); done(); } ); }); 环境变量的值等于process.env.PERFORMANCE_TEST时运行单元测试,结果:

'true'

未设置PERFORMANCE_TEST=true npm t -- /Users/elsa/workspace/github.com/mrdulin/jest-codelab/src/stackoverflow/58264344/index.spec.t > jest-codelab@1.0.0 test /Users/elsa/workspace/github.com/mrdulin/jest-codelab > jest --detectOpenHandles "/Users/elsa/workspace/github.com/mrdulin/jest-codelab/src/stackoverflow/58264344/index.spec.ts" PASS src/stackoverflow/58264344/index.spec.ts test suites ✓ functional-approach-2 perforance test (18ms) console.info src/stackoverflow/58264344/index.spec.ts:22 Functional Approach 2 Performance Test console.log src/stackoverflow/58264344/index.spec.ts:4 0 console.info src/stackoverflow/58264344/index.spec.ts:28 Length: 0 console.info src/stackoverflow/58264344/index.spec.ts:29 Time Taken: 5ms Test Suites: 1 passed, 1 total Tests: 1 passed, 1 total Snapshots: 0 total Time: 5.67s, estimated 9s 环境变量的值时,请勿运行单元测试:

process.env.PERFORMANCE_TEST