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.
Jest states in docs: "Jest virtualizes JavaScript environments and runs tests in parallel across worker processes."
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.
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.
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.
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.
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);
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With