Skip to content

Commit 8ae2404

Browse files
authored
Add unit tests parallelization (Synthetixio#1272)
* Add --parallel flag to npm run test command It can be used with 'npm run test -- --parallel' * Add mocha override on hardhat test task * Add parallelism for unit tests on ci config * add --jobs flag to npm run tests command defaulted to 4 * update mocha tests timeout from 30secs to 60 secs
1 parent 4b9b2ee commit 8ae2404

File tree

4 files changed

+34
-5
lines changed

4 files changed

+34
-5
lines changed

.circleci/config.yml

+4-1
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ jobs:
248248
username: $DOCKERHUB_USERNAME
249249
password: $DOCKERHUB_TOKEN
250250
resource_class: large
251+
parallelism: 8
251252
steps:
252253
- checkout
253254
- attach_workspace:
@@ -257,7 +258,9 @@ jobs:
257258
name: Test and output gas used
258259
command: |
259260
set +e
260-
npm test
261+
circleci tests glob 'test/contracts/*.js' |
262+
circleci tests split |
263+
xargs npm test
261264
EXIT_CODE=$?
262265
cat test-gas-used.log
263266
printf "\\n"

.circleci/src/jobs/job-unit-tests.yml

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Runs all unit and spec tests
22
{{> job-header.yml}}
33
resource_class: large
4+
parallelism: 8
45
steps:
56
- checkout
67
- attach_workspace:
@@ -10,7 +11,9 @@ steps:
1011
name: Test and output gas used
1112
command: |
1213
set +e
13-
npm test
14+
circleci tests glob 'test/contracts/*.js' |
15+
circleci tests split |
16+
xargs npm test
1417
EXIT_CODE=$?
1518
cat test-gas-used.log
1619
printf "\\n"

hardhat.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,6 @@ module.exports = {
6565
outputFile: 'test-gas-used.log',
6666
},
6767
mocha: {
68-
timeout: 30e3, // 30s
68+
timeout: 60e3, // 60s
6969
},
7070
};

hardhat/tasks/task-test.js

+25-2
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,45 @@
1-
const { task } = require('hardhat/config');
1+
const Mocha = require('mocha');
2+
const { types, task, subtask } = require('hardhat/config');
3+
const { TASK_TEST_RUN_MOCHA_TESTS } = require('hardhat/builtin-tasks/task-names');
24
const { gray, yellow } = require('chalk');
35
const optimizeIfRequired = require('../util/optimizeIfRequired');
46

7+
// Override builtin "test:run-mocha-tests" subtask so we can use the local mocha
8+
// installation, which is up to date and allows us to run parallel tests.
9+
subtask(TASK_TEST_RUN_MOCHA_TESTS).setAction(async ({ testFiles }, { config }) => {
10+
const mocha = new Mocha(config.mocha);
11+
testFiles.forEach(file => mocha.addFile(file));
12+
13+
const testFailures = await new Promise(resolve => {
14+
mocha.run(resolve);
15+
});
16+
17+
return testFailures;
18+
});
19+
520
task('test')
621
.addFlag('optimizer', 'Compile with the optimizer')
722
.addFlag('gas', 'Compile gas usage')
823
.addFlag('native', 'Compile with the native solc compiler')
24+
.addFlag('parallel', 'Run tests in parallel')
25+
.addOptionalParam('jobs', 'Max number of worker processes for parallel runs', 4, types.int)
926
.addOptionalParam('gasOutputFile', 'Gas reporter output file')
1027
.addOptionalParam('grep', 'Filter tests to only those with given logic')
1128
.setAction(async (taskArguments, hre, runSuper) => {
12-
const { gas, grep, native, gasOutputFile } = taskArguments;
29+
const { gas, grep, native, gasOutputFile, parallel, jobs } = taskArguments;
1330

1431
if (native) {
1532
hre.config.solc.native = true;
1633
}
1734

1835
optimizeIfRequired({ hre, taskArguments });
1936

37+
if (parallel) {
38+
console.log(gray(`Running tests in parallel. Jobs count: ${jobs}`));
39+
hre.config.mocha.parallel = true;
40+
hre.config.mocha.jobs = jobs;
41+
}
42+
2043
if (grep) {
2144
console.log(gray('Filtering tests to those containing'), yellow(grep));
2245
hre.config.mocha.grep = grep;

0 commit comments

Comments
 (0)