Skip to content

Commit

Permalink
Run tests twice (#2140)
Browse files Browse the repository at this point in the history
Fix reruns of `test` in scripts. This is a mocha caching and cleanup issue addressed here: mochajs/mocha#2783.

Multiple runs of the test task within a hardhat script are now enabled. The resolution is `mocha.dispose` at the end of a run to clear up state.

The mocha dispose method was added in mocha@7.2.0 but the @types/mocha doesn't reflect it till `9.0.0`. This commit:

* bumps mocha as a dep in hardhat-core to 7.2.0 to be explicit
* bumps mocha as a dev-dep everywhere else to 7.2.0 for consistency in our local dev environment
* bumps @types/mocha to 9 in dev-deps in all packages to allow use of dispose and consistency across packages - no code changes are required to support this. Note that the sample typescript project already provides a default @types/mocha on version 9.0.0

Relates to #1720
  • Loading branch information
kanej authored Dec 15, 2021
1 parent c8eefd9 commit 0a5ab4f
Show file tree
Hide file tree
Showing 21 changed files with 98 additions and 36 deletions.
5 changes: 5 additions & 0 deletions .changeset/thirty-olives-laugh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"hardhat": patch
---

Fix running the `test` task multiple times in a script (issue #1720)
4 changes: 2 additions & 2 deletions packages/e2e/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"dependencies": {
"@types/chai": "^4.2.0",
"@types/fs-extra": "^5.1.0",
"@types/mocha": "^5.2.6",
"@types/mocha": "^9.0.0",
"@types/node": "^12.0.0",
"@types/shelljs": "^0.8.6",
"@typescript-eslint/eslint-plugin": "4.29.2",
Expand All @@ -32,7 +32,7 @@
"eslint-plugin-import": "2.24.1",
"eslint-plugin-prettier": "3.4.0",
"fs-extra": "^7.0.1",
"mocha": "^7.1.2",
"mocha": "^7.2.0",
"prettier": "2.4.1",
"rimraf": "^3.0.2",
"shelljs": "^0.8.3",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// eslint-disable-next-line import/no-extraneous-dependencies
const hre = require("hardhat");

async function main() {
const code = await hre.run("test");

if (code > 0) {
console.error("Failed first test run");
process.exit(1);
}

const secondCode = await hre.run("test");

if (secondCode > 0) {
console.error("Failed second test run");
process.exit(1);
}
}

main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
describe("simple", () => {
it("should pass", () => {});
});
27 changes: 27 additions & 0 deletions packages/e2e/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,33 @@ describe("e2e tests", function () {
const { code: hhCleanCode2 } = shell.exec(`${hardhatBinary} clean`);
assert.equal(hhCleanCode2, 0);
});

it("should test programmatically", function () {
// hh clean
const { code: hhCleanCode1 } = shell.exec(`${hardhatBinary} clean`);
assert.equal(hhCleanCode1, 0);

// hh compile
const { code: testRunCode, stdout } = shell.exec(
`${hardhatBinary} run ./scripts/multi-run-test.js`
);
assert.equal(testRunCode, 0);

// check stdout

// check we get passing runs
assert.match(stdout, /1 passing/);
// check we get no runs without tests
assert.notMatch(
stdout,
/0 passing/,
"A test run occured with 0 tests - potential caching issue"
);

// hh clean
const { code: hhCleanCode2 } = shell.exec(`${hardhatBinary} clean`);
assert.equal(hhCleanCode2, 0);
});
});

describe("sample projects", function () {
Expand Down
6 changes: 3 additions & 3 deletions packages/hardhat-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
"@types/fs-extra": "^5.1.0",
"@types/glob": "^7.1.1",
"@types/lodash": "^4.14.123",
"@types/mocha": "^5.2.6",
"@types/mocha": "^9.0.0",
"@types/node": "^12.0.0",
"@types/node-fetch": "^2.3.7",
"@types/qs": "^6.5.3",
Expand All @@ -81,7 +81,7 @@
"eslint-plugin-import": "2.24.1",
"eslint-plugin-prettier": "3.4.0",
"ethers": "^5.0.0",
"mocha": "^7.1.2",
"mocha": "^7.2.0",
"prettier": "2.4.1",
"proxy": "^1.0.2",
"rimraf": "^3.0.2",
Expand Down Expand Up @@ -124,7 +124,7 @@
"lodash": "^4.17.11",
"merkle-patricia-tree": "^4.2.0",
"mnemonist": "^0.38.0",
"mocha": "^7.1.2",
"mocha": "^7.2.0",
"node-fetch": "^2.6.0",
"qs": "^6.7.0",
"raw-body": "^2.4.1",
Expand Down
2 changes: 2 additions & 0 deletions packages/hardhat-core/src/builtin-tasks/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ subtask(TASK_TEST_RUN_MOCHA_TESTS)
mocha.run(resolve);
});

mocha.dispose();

return testFailures;
});

Expand Down
4 changes: 2 additions & 2 deletions packages/hardhat-docker/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"@types/chai": "^4.2.0",
"@types/dockerode": "^2.5.19",
"@types/fs-extra": "^5.1.0",
"@types/mocha": "^5.2.6",
"@types/mocha": "^9.0.0",
"@types/node-fetch": "^2.3.7",
"@types/node": "^12.0.0",
"@typescript-eslint/eslint-plugin": "4.29.2",
Expand All @@ -41,7 +41,7 @@
"eslint-config-prettier": "8.3.0",
"eslint-plugin-import": "2.24.1",
"eslint-plugin-prettier": "3.4.0",
"mocha": "^7.1.2",
"mocha": "^7.2.0",
"prettier": "2.4.1",
"rimraf": "^3.0.2",
"ts-node": "^8.1.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/hardhat-ethers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
],
"devDependencies": {
"@types/chai": "^4.2.0",
"@types/mocha": "^5.2.6",
"@types/mocha": "^9.0.0",
"@types/node": "^12.0.0",
"@typescript-eslint/eslint-plugin": "4.29.2",
"@typescript-eslint/parser": "4.29.2",
Expand All @@ -49,7 +49,7 @@
"eslint-plugin-prettier": "3.4.0",
"ethers": "^5.0.0",
"hardhat": "^2.0.0",
"mocha": "^7.1.2",
"mocha": "^7.2.0",
"prettier": "2.4.1",
"rimraf": "^3.0.2",
"ts-node": "^8.1.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/hardhat-etherscan/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"@types/cbor": "^5.0.1",
"@types/chai": "^4.2.0",
"@types/fs-extra": "^5.1.0",
"@types/mocha": "^5.2.6",
"@types/mocha": "^9.0.0",
"@types/node-fetch": "^2.3.7",
"@types/node": "^12.0.0",
"@types/semver": "^6.0.2",
Expand All @@ -61,7 +61,7 @@
"eslint-plugin-prettier": "3.4.0",
"ethers": "^5.0.8",
"hardhat": "^2.0.4",
"mocha": "^7.1.2",
"mocha": "^7.2.0",
"nock": "^13.0.5",
"prettier": "2.4.1",
"rimraf": "^3.0.2",
Expand Down
4 changes: 2 additions & 2 deletions packages/hardhat-ganache/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"@types/chai": "^4.2.0",
"@types/debug": "^4.1.4",
"@types/fs-extra": "^5.1.0",
"@types/mocha": "^5.2.6",
"@types/mocha": "^9.0.0",
"@types/node": "^12.0.0",
"@typescript-eslint/eslint-plugin": "4.29.2",
"@typescript-eslint/parser": "4.29.2",
Expand All @@ -50,7 +50,7 @@
"eslint-plugin-import": "2.24.1",
"eslint-plugin-prettier": "3.4.0",
"hardhat": "^2.0.0",
"mocha": "^7.1.2",
"mocha": "^7.2.0",
"prettier": "2.4.1",
"rimraf": "^3.0.2",
"ts-interface-builder": "^0.2.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/hardhat-shorthand/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"devDependencies": {
"@types/chai": "^4.2.0",
"@types/fs-extra": "^5.1.0",
"@types/mocha": "^5.2.6",
"@types/mocha": "^9.0.0",
"@types/node": "^12.0.0",
"@typescript-eslint/eslint-plugin": "4.29.2",
"@typescript-eslint/parser": "4.29.2",
Expand All @@ -44,7 +44,7 @@
"eslint-plugin-import": "2.24.1",
"eslint-plugin-prettier": "3.4.0",
"hardhat": "^2.0.0",
"mocha": "^7.1.2",
"mocha": "^7.2.0",
"prettier": "2.4.1",
"rimraf": "^3.0.2",
"ts-node": "^8.1.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/hardhat-solhint/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"devDependencies": {
"@types/chai": "^4.2.0",
"@types/fs-extra": "^5.1.0",
"@types/mocha": "^5.2.6",
"@types/mocha": "^9.0.0",
"@types/node": "^12.0.0",
"@typescript-eslint/eslint-plugin": "4.29.2",
"@typescript-eslint/parser": "4.29.2",
Expand All @@ -49,7 +49,7 @@
"eslint-plugin-prettier": "3.4.0",
"fs-extra": "^7.0.1",
"hardhat": "^2.0.0",
"mocha": "^7.1.2",
"mocha": "^7.2.0",
"prettier": "2.4.1",
"rimraf": "^3.0.2",
"ts-node": "^8.1.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/hardhat-solpp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"devDependencies": {
"@types/chai": "^4.2.0",
"@types/fs-extra": "^5.1.0",
"@types/mocha": "^5.2.6",
"@types/mocha": "^9.0.0",
"@types/node": "^12.0.0",
"@typescript-eslint/eslint-plugin": "4.29.2",
"@typescript-eslint/parser": "4.29.2",
Expand All @@ -49,7 +49,7 @@
"eslint-plugin-import": "2.24.1",
"eslint-plugin-prettier": "3.4.0",
"hardhat": "^2.0.0",
"mocha": "^7.1.2",
"mocha": "^7.2.0",
"prettier": "2.4.1",
"rimraf": "^3.0.2",
"ts-node": "^8.1.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/hardhat-truffle4/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"@nomiclabs/hardhat-web3-legacy": "^2.0.0",
"@types/fs-extra": "^5.1.0",
"@types/glob": "^7.1.1",
"@types/mocha": "^5.2.6",
"@types/mocha": "^9.0.0",
"@types/node": "^12.0.0",
"@typescript-eslint/eslint-plugin": "4.29.2",
"@typescript-eslint/parser": "4.29.2",
Expand All @@ -52,7 +52,7 @@
"eslint-plugin-import": "2.24.1",
"eslint-plugin-prettier": "3.4.0",
"hardhat": "^2.6.4",
"mocha": "^7.1.2",
"mocha": "^7.2.0",
"prettier": "2.4.1",
"rimraf": "^3.0.2",
"ts-node": "^8.1.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/hardhat-truffle5/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"@nomiclabs/hardhat-web3": "^2.0.0",
"@types/fs-extra": "^5.1.0",
"@types/glob": "^7.1.1",
"@types/mocha": "^5.2.6",
"@types/mocha": "^9.0.0",
"@types/node": "^12.0.0",
"@typescript-eslint/eslint-plugin": "4.29.2",
"@typescript-eslint/parser": "4.29.2",
Expand All @@ -52,7 +52,7 @@
"eslint-plugin-import": "2.24.1",
"eslint-plugin-prettier": "3.4.0",
"hardhat": "^2.6.4",
"mocha": "^7.1.2",
"mocha": "^7.2.0",
"prettier": "2.4.1",
"rimraf": "^3.0.2",
"ts-node": "^8.1.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/hardhat-vyper/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"@types/chai": "^4.2.0",
"@types/fs-extra": "^5.1.0",
"@types/glob": "^7.1.1",
"@types/mocha": "^5.2.6",
"@types/mocha": "^9.0.0",
"@types/node": "^12.0.0",
"@typescript-eslint/eslint-plugin": "4.29.2",
"@typescript-eslint/parser": "4.29.2",
Expand All @@ -50,7 +50,7 @@
"eslint-plugin-import": "2.24.1",
"eslint-plugin-prettier": "3.4.0",
"hardhat": "^2.0.0",
"mocha": "^7.1.2",
"mocha": "^7.2.0",
"prettier": "2.4.1",
"rimraf": "^3.0.2",
"ts-node": "^8.1.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/hardhat-waffle/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"@nomiclabs/hardhat-ethers": "^2.0.0",
"@types/chai": "^4.2.0",
"@types/fs-extra": "^5.1.0",
"@types/mocha": "^5.2.6",
"@types/mocha": "^9.0.0",
"@types/node": "^12.0.0",
"@typescript-eslint/eslint-plugin": "4.29.2",
"@typescript-eslint/parser": "4.29.2",
Expand All @@ -46,7 +46,7 @@
"ethereum-waffle": "^3.2.0",
"ethers": "^5.0.0",
"hardhat": "^2.0.0",
"mocha": "^7.1.2",
"mocha": "^7.2.0",
"prettier": "2.4.1",
"rimraf": "^3.0.2",
"ts-node": "^8.1.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/hardhat-web3-legacy/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
],
"devDependencies": {
"@types/chai": "^4.2.0",
"@types/mocha": "^5.2.6",
"@types/mocha": "^9.0.0",
"@types/node": "^12.0.0",
"@typescript-eslint/eslint-plugin": "4.29.2",
"@typescript-eslint/parser": "4.29.2",
Expand All @@ -43,7 +43,7 @@
"eslint-plugin-import": "2.24.1",
"eslint-plugin-prettier": "3.4.0",
"hardhat": "^2.0.0",
"mocha": "^7.1.2",
"mocha": "^7.2.0",
"prettier": "2.4.1",
"rimraf": "^3.0.2",
"ts-node": "^8.1.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/hardhat-web3/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
],
"devDependencies": {
"@types/chai": "^4.2.0",
"@types/mocha": "^5.2.6",
"@types/mocha": "^9.0.0",
"@types/node": "^12.0.0",
"@typescript-eslint/eslint-plugin": "4.29.2",
"@typescript-eslint/parser": "4.29.2",
Expand All @@ -43,7 +43,7 @@
"eslint-plugin-import": "2.24.1",
"eslint-plugin-prettier": "3.4.0",
"hardhat": "^2.0.0",
"mocha": "^7.1.2",
"mocha": "^7.2.0",
"prettier": "2.4.1",
"rimraf": "^3.0.2",
"ts-node": "^8.1.0",
Expand Down
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2147,10 +2147,10 @@
dependencies:
"@types/node" "*"

"@types/mocha@^5.2.6":
version "5.2.7"
resolved "https://registry.yarnpkg.com/@types/mocha/-/mocha-5.2.7.tgz#315d570ccb56c53452ff8638738df60726d5b6ea"
integrity sha512-NYrtPht0wGzhwe9+/idPaBB+TqkY9AhTvOLMkThm0IoEfLaiVQZwBwyJ5puCkO3AUCWrmcoePjp2mbFocKy4SQ==
"@types/mocha@^9.0.0":
version "9.0.0"
resolved "https://registry.yarnpkg.com/@types/mocha/-/mocha-9.0.0.tgz#3205bcd15ada9bc681ac20bef64e9e6df88fd297"
integrity sha512-scN0hAWyLVAvLR9AyW7HoFF5sJZglyBsbPuHO4fv7JRvfmPBMfp1ozWqOf/e4wwPNxezBZXRfWzMb6iFLgEVRA==

"@types/node-fetch@^2.3.7", "@types/node-fetch@^2.5.5":
version "2.5.11"
Expand Down Expand Up @@ -10476,7 +10476,7 @@ mnemonist@^0.38.0:
dependencies:
obliterator "^1.6.1"

mocha@^7.1.2:
mocha@^7.2.0:
version "7.2.0"
resolved "https://registry.yarnpkg.com/mocha/-/mocha-7.2.0.tgz#01cc227b00d875ab1eed03a75106689cfed5a604"
integrity sha512-O9CIypScywTVpNaRrCAgoUnJgozpIofjKUYmJhiCIJMiuYnLI6otcb1/kpW9/n/tJODHGZ7i8aLQoDVsMtOKQQ==
Expand Down

0 comments on commit 0a5ab4f

Please sign in to comment.