Skip to content
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

Run tests twice #2140

Merged
merged 2 commits into from
Dec 15, 2021
Merged

Run tests twice #2140

merged 2 commits into from
Dec 15, 2021

Conversation

kanej
Copy link
Member

@kanej kanej commented Dec 8, 2021

This is a mocha caching and cleanup issue addressed here: mochajs/mocha#2783.

Relates to #1720

Testing

An integration test has been added to e2e to cover the run twice scenario and guard against regression of the bug.

Manual

To test manually:

  1. create a new advanced typescript hardhat project
  2. link to `hardhat-core
  3. create a new scripts file ./scripts/multi-run.js:
// 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);
  }

  console.log("\nFinished and complete\n");
}

main()
  .then(() => process.exit(0))
  .catch((error) => {
    console.error(error);
    process.exit(1);
  });
  1. Run the script npx hardhat run ./scripts/multi-run.js
  2. Confirm the script runs without error and that tests are present in both runs:

image

As a test against a more in-depth test suite, I did the equivalent of the above on the open-zepplin test suite (updating to the latest hardhat libs first) and was able to run the suite twice programmatically with the script above.

TODO

  • Apply fix from mocha suggestions
  • Look at options on unit tests
  • Run against existing project with large test suite

@kanej kanej self-assigned this Dec 8, 2021
@changeset-bot
Copy link

changeset-bot bot commented Dec 8, 2021

🦋 Changeset detected

Latest commit: 80a4437

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
hardhat Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@kanej kanej linked an issue Dec 8, 2021 that may be closed by this pull request
@alcuadrado
Copy link
Member

This is really interesting! This new API of mocha is something I wasn't expecting tbh.

I think the decision we need to make is if we are ok with bumping the mocha dependency. If we do, I think we should:

  1. Document that we may do it in our stability guarantees docs.
  2. Bump the minor version.
  3. Make sure that changes between 5 and 9 aren't too aggressive.
  4. Make sure mocha 9 supports all the node.js versions than we do.

@kanej
Copy link
Member Author

kanej commented Dec 9, 2021

This is really interesting! This new API of mocha is something I wasn't expecting tbh.

I think the decision we need to make is if we are ok with bumping the mocha dependency. If we do, I think we should:

1. Document that we may do it in our stability guarantees docs.

2. Bump the minor version.

3. Make sure that changes between 5 and 9 aren't too aggressive.

4. Make sure mocha 9 supports all the node.js versions than we do.

This fix is adding mocha.dispose after run in the test task. This resolves the problem in local testing for a simple project - more testing is needed on larger test suites.

dispose was introduced in mocha 7.2.0 changelog as part of Mocha's multi-run fix (mochajs/mocha#4234).

Hardhat-core has a dependency on ^7.1.2 which get resolved to 7.2.0 - so dispose is present in a local build. dispose is not present in our current @types/mocha however, and you have to bump the version to 9 to get it.

So in this PR I propose:

  • bump mocha as a dep in hardhat-core to 7.2.0 to be explicit
  • bump mocha as a dev-dep everywhere else to 7.2.0 for consistency in our local dev environment
  • bump @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

I think moving the dep in hardhat-core from ^7.1.2 to 7.2.0 is safe and explicit and doesn't require a change to our stability promises. From the package.json mocha 7.2 supports node 8.10 and above:

image

@alcuadrado @fvictorio

@alcuadrado
Copy link
Member

That's a good find! I think that makes sense

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 [email protected] 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
@kanej kanej marked this pull request as ready for review December 10, 2021 11:23
@kanej kanej requested a review from fvictorio December 10, 2021 11:23
@kanej
Copy link
Member Author

kanej commented Dec 10, 2021

I have pulled the open-zepplin repo and done a multi-test script using this branch, and the their suite ran twice successfully with all tests.

If we are happy with the version bumps this PR is ready for review.

Copy link
Member

@fvictorio fvictorio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just one very minor comment, otherwise LGTM

@kanej kanej merged commit 0a5ab4f into master Dec 15, 2021
@kanej kanej deleted the run-tests-twice branch December 15, 2021 16:18
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 18, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Tests can't be run programmatically more than once
3 participants