trufflesuite / truffle

:warning: The Truffle Suite is being sunset. For information on ongoing support, migration options and FAQs, visit the Consensys blog. Thank you for all the support over the years.
https://consensys.io/blog/consensys-announces-the-sunset-of-truffle-and-ganache-and-new-hardhat?utm_source=github&utm_medium=referral&utm_campaign=2023_Sep_truffle-sunset-2023_announcement_
MIT License
14.02k stars 2.32k forks source link

Mocha globals in exec to iterate quickly on tests (or no-compile, no-deploy option for test) #2818

Open jjgonecrypto opened 4 years ago

jjgonecrypto commented 4 years ago

Issue

I'd like to know if there is a way to execute tests in truffle console (or otherwise) without having to restart ganache and redeploy between test runs. This is imperative to speed up the feedback loop when iterating on test specs when there are no changes to the underlying Solidity files. (Note: we are employing snapshot and restores before and after each test respectively).

Steps to Reproduce

  1. truffle console
  2. > deploy
  3. > exec singleTest.js
  4. repeat 3

Expected Behavior

I'm looking for a way to run step 3 with mocha injected, with the same globals available (web3, contract, it, before(Each)?|after(Each)?, that way I don't have to rewrite tests in a generic way for exec and then port them back to regular specs when done.

An alternative might be adding flags to the test command as in: > test --no-compile --no-deploy singleTest.js - but that's a feature request I suspect.

Actual Results

exec won't setup testing globals and test will always try to recompile and redeploy, adding on redundant wait time to the testing feedback loop

Environment

gnidan commented 4 years ago

Thanks for raising this issue!

We've been discussing this issue a bunch recently. Note that there are edge cases, if your tests alter your contract state. We're looking at solutions for allowing this special-case behavior in a "buyer beware" way (i.e., we can't solve the edge cases, but maybe your tests aren't affected by those)

jjgonecrypto commented 4 years ago

Thanks @gnidan . We are using evm_snapshot on beforeEach and evm_revert on afterEach so tests altering contract state is not an issue.

sirpy commented 4 years ago

plus one... i think allowing exec to run tests would be perfect make truffle test keep cleanroom behaviour, but use exec for quick iterations when doing TDD