-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add --parallel
, --bail
and --grep
params to the test task
#2359
Changes from 9 commits
b97808f
de16fcc
f498fc7
ee68191
368c8bf
c972d67
983f554
345ebab
ac001a4
30c1b7d
057dcdf
fe479e0
b399960
7feb7f3
19df511
bf6417e
60def3a
b74ee2e
84b070c
3917ebe
3047a83
8ba2070
36411fd
2aa70d4
7e8208a
7a6d522
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
"hardhat": minor | ||
--- | ||
|
||
The `test` task now supports a `--parallel` flag to run tests in parallel. There are also two other new flags: `--bail`, to stop the execution after the first test failure, and `--grep`, to filter which tests should be run. | ||
|
||
To support running tests in parallel, the version of `mocha` used by Hardhat was upgraded to its latest version. This should be a mostly backward-compatible change, but there could be some edge cases where this breaks existing tests. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
describe("simple", () => { | ||
it("should pass", () => {}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import chalk from "chalk"; | ||
import type { MochaOptions } from "mocha"; | ||
alcuadrado marked this conversation as resolved.
Show resolved
Hide resolved
|
||
import path from "path"; | ||
|
||
import { HARDHAT_NETWORK_NAME } from "../internal/constants"; | ||
|
@@ -43,24 +44,59 @@ subtask(TASK_TEST_GET_TEST_FILES) | |
subtask(TASK_TEST_SETUP_TEST_ENVIRONMENT, async () => {}); | ||
|
||
subtask(TASK_TEST_RUN_MOCHA_TESTS) | ||
.addFlag("parallel", "Run tests in parallel") | ||
.addFlag("bail", "Stop running tests after the first test failure") | ||
.addOptionalParam("grep", "Only run tests matching the given regexp") | ||
fvictorio marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.addOptionalVariadicPositionalParam( | ||
"testFiles", | ||
"An optional list of files to test", | ||
[] | ||
) | ||
.setAction(async ({ testFiles }: { testFiles: string[] }, { config }) => { | ||
const { default: Mocha } = await import("mocha"); | ||
const mocha = new Mocha(config.mocha); | ||
testFiles.forEach((file) => mocha.addFile(file)); | ||
.setAction( | ||
async ( | ||
{ | ||
bail, | ||
parallel, | ||
testFiles, | ||
grep, | ||
}: { | ||
bail: boolean; | ||
parallel: boolean; | ||
testFiles: string[]; | ||
grep?: string; | ||
}, | ||
{ config } | ||
) => { | ||
const { default: Mocha } = await import("mocha"); | ||
|
||
const testFailures = await new Promise<number>((resolve) => { | ||
mocha.run(resolve); | ||
}); | ||
const mochaConfig: MochaOptions = { | ||
...config.mocha, | ||
bail, | ||
grep, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should/do we allow these to be configured in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, I hadn't noticed that, thanks for pointing it out. I actually think that would be the correct behavior though? Normally CLI flags override what's in the config (for example, that's what Prettier does by default). But it's good to know that this is the case. I will add a comment explaining that this is intentional. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wait I see what you mean. For the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great catch! I was also tricked by this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. |
||
}; | ||
|
||
mocha.dispose(); | ||
if (parallel) { | ||
mochaConfig.parallel = true; | ||
|
||
return testFailures; | ||
}); | ||
const mochaRequire = mochaConfig.require ?? []; | ||
if (!mochaRequire.includes("hardhat/register")) { | ||
mochaRequire.push("hardhat/register"); | ||
} | ||
mochaConfig.require = mochaRequire; | ||
} | ||
|
||
const mocha = new Mocha(mochaConfig); | ||
testFiles.forEach((file) => mocha.addFile(file)); | ||
|
||
const testFailures = await new Promise<number>((resolve) => { | ||
mocha.run(resolve); | ||
}); | ||
|
||
mocha.dispose(); | ||
|
||
return testFailures; | ||
} | ||
); | ||
|
||
subtask(TASK_TEST_RUN_SHOW_FORK_RECOMMENDATIONS).setAction( | ||
async (_, { config, network }) => { | ||
|
@@ -80,14 +116,23 @@ task(TASK_TEST, "Runs mocha tests") | |
[] | ||
) | ||
.addFlag("noCompile", "Don't compile before running this task") | ||
.addFlag("parallel", "Run tests in parallel") | ||
.addFlag("bail", "Stop running tests after the first test failure") | ||
.addOptionalParam("grep", "Only run tests matching the given regexp") | ||
fvictorio marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.setAction( | ||
async ( | ||
{ | ||
testFiles, | ||
noCompile, | ||
parallel, | ||
bail, | ||
grep, | ||
}: { | ||
testFiles: string[]; | ||
noCompile: boolean; | ||
parallel: boolean; | ||
bail: boolean; | ||
grep?: string; | ||
}, | ||
{ run, network } | ||
) => { | ||
|
@@ -103,6 +148,9 @@ task(TASK_TEST, "Runs mocha tests") | |
|
||
const testFailures = await run(TASK_TEST_RUN_MOCHA_TESTS, { | ||
testFiles: files, | ||
parallel, | ||
bail, | ||
grep, | ||
}); | ||
|
||
if (network.name === HARDHAT_NETWORK_NAME) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd move this to its own guide, to make it more visible.
Also, this is not a waffle thing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.