Search code examples
jestjsperformance-testing

Conditionally run tests in 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();       
}); 

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

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

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.

What's the best way to do this?


Solution

  • const itif = (condition) => condition ? it : it.skip;
    
    describe('suite name', () => {
      itif(true)('test name', async () => {
        // Your test
      });
    });