page = await browser.newPage();
In that case console logs can be enabled using method:
Helpers.pageSetup(page);
By default Jest runs tests in parallel with a worker pool of child processes.
The console commands responsible for parallelization settings (Official Jest documentation):
- --maxConcurrency=
(Default: 5) Prevents Jest from executing more than the specified amount of tests at the same time. Only affects tests that use test.concurrent.- --maxWorkers=
| Specifies the maximum number of workers the worker-pool will spawn for running tests. In single run mode, this defaults to the number of the cores available on your machine minus one for the main thread. In watch mode, this defaults to half of the available cores on your machine to ensure Jest is unobtrusive and does not grind your machine to a halt. It may be useful to adjust this in resource limited environments like CIs but the defaults should be adequate for most use-cases. For environments with variable CPUs available, you can use percentage based configuration: --maxWorkers=50%
Create at least 21 test files and invoke with --maxWorkers=4 to run tests on 4 threads for example. That should be enough to trick Jest into running your tests always in parallel. (Source)
Running with --no-cache it always seems to run in parallel (as long as there are at least two files anyway). (Source)
There is possibility to add several Jest projects (Official Jest documentation). In case there is need to have E2E tests and Unit tests in one solution, or one project to run tests sequentially and another project to run tests in parallel. This can be helpful for setuping CI/CD.
Example of parallel and sequential projects run:
jest.config.js
example:
module.exports = {
projects: [
{
displayName: "default-tests",
globalSetup: "./test-environment/setup.js",
globalTeardown: "./test-environment/teardown.js",
testEnvironment: "./test-environment/environment.js",
setupFilesAfterEnv: ["./test-environment/jest.setup.js"],
transformIgnorePatterns: ["node_modules/(?!(test-juggler)/)"],
verbose: true,
testTimeout: 60000
},
{
displayName: "serial-tests",
testMatch: ["**/?(*.)+(serial-test).[jt]s?(x)"],
globalSetup: "./test-environment/setup.js",
globalTeardown: "./test-environment/teardown.js",
testEnvironment: "./test-environment/environment.js",
setupFilesAfterEnv: ["./test-environment/jest.setup.js"],
transformIgnorePatterns: ["node_modules/(?!(test-juggler)/)"],
verbose: true,
testTimeout: 60000
}
],
reporters: ["default", ["jest-junit", { outputDirectory: "junit-report" }]]
};
Add several scripts in package.json
file under "scripts":
...
"default-tests": "jest --selectProjects default-tests",
"serial-tests": "jest --selectProjects serial-tests --runInBand",
"all-tests": "npm run default-tests && npm run serial-tests",
...
Then:
npm run default-tests
to run all default testsnpm run serial-tests
to run all serial testsnpm run all-tests
to run all projects in one batch (serial tests will run sequentially)npm run test
to run all projects in one batch (all tests in parallel)Note 1: Parallel and sequential run can be implemented without projects. Add test files matchers in jest scripts. Example:
...
"serial-tests": "jest --testRegex '.*serial-test*' --runInBand",
"all-tests": "npm run test && npm run serial-tests",
...
or
...
"all-tests": "jest && jest --testRegex '.*serial-test*' --runInBand",
...
Note 2: When running two scripts at once then latest test run overrides junit report. So user is unable to find information about first run. To resolve this problem and generate unique report after each test run add setting uniqueOutputName
to jest-junit configuration in jest.config.js
file:
reporters: ["default", ["jest-junit", { outputDirectory: "junit-report", uniqueOutputName: "true" }]]