Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run same test multiple times using jest

Tags:

node.js

jestjs

I am using jest to run my test suite (with back-end as Node.js & express). Below is my code:

const puppeteer = require ('puppeteer');

test('testing login function', async () => {
    const browser = await puppeteer.launch ({  
        headless: true,
        args: ['--no-sandbox']
  });

    const page = await browser.newPage();

    await page.type('#username', 'admin');
    await page.type('#password', 'password');

    await page.click('login-button');

    await page.waitFor('.card');

    expect(texthead).toEqual('Welcome to webpage');

    await browser.close();
});

I am trying to run this same test multiple times at once, is there a way using it by jest, or maybe using other tools.

like image 251
Arghajeet Bhattacharya Avatar asked Sep 13 '18 17:09

Arghajeet Bhattacharya


People also ask

Do Jest tests run concurrently?

Jest states in docs: "Jest virtualizes JavaScript environments and runs tests in parallel across worker processes."

Does Jest support parallel execution?

Each time a test run completes, the global environment is automatically reset for the next. Since tests are standalone and their execution order doesn't matter, Jest runs tests in parallel.

What can Jest reliably run tests in?

Fast and safe By ensuring your tests have unique global state, Jest can reliably run tests in parallel. To make things quick, Jest runs previously failed tests first and re-organizes runs based on how long test files take.

How do I run a specific Jest test?

So to run a single test, there are two approaches: Option 1: If your test name is unique, you can enter t while in watch mode and enter the name of the test you'd like to run. Option 2: Hit p while in watch mode to enter a regex for the filename you'd like to run.


2 Answers

If you're looking for something that will essentially stress test a single test (looking for test flakiness like I was) then you could use the following one-liner in your terminal, which of course uses bash and Jest together.

for i in {1..100}; do npx jest <test_file> --silent || (echo 'Failed after $i attempts' && break); done

This specific command requires you to have npx installed and it uses the --silent option but you can change it as you please.

like image 139
Brady Dowling Avatar answered Oct 21 '22 10:10

Brady Dowling


If you don't want tests run sequentially, you can use Promise.all. Here is a quick example of how you could refactor your code.

const runTheTest = async () => { 
  const browser = await puppeteer.launch ({
    headless: true, args: ['--no-sandbox'] 
  });
  .......
  return browser.close();
}

test('testing login function', async () => { 
  const testRuns = []
  for (let index = 0; index < NUMBER_OF_RUNS; index++) {
    testRuns.push(runTheTest())
  }
  return Promise.all(testRuns);
})
like image 6
Herman Starikov Avatar answered Oct 21 '22 10:10

Herman Starikov