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.03k stars 2.32k forks source link

Mocha compatibility #1228

Open krzkaczor opened 6 years ago

krzkaczor commented 6 years ago

Advantages

All advantages that I can see are around tooling support (mostly IDEs and editors). For example, things that would work out of the box if it was pure mocha:

Implementation problems

The biggest problem right now is that without workarounds like mocha-prepare or changes to mocha itself it's impossible to run all required async tasks before running tests itself.

Another problem, of course, is that truffle uses contract functions to describe tests suites instead of relying on describe. I am curious what's the reasoning behind it and could it be easily replaced with describe?

Is there any pressure for bringing full compatibility with mocha?

cgewecke commented 6 years ago

Hi @krzkaczor. Truffle does expose / consume a mocha object in truffle.js where you can set mocha options.

The syntax is:

module.exports = {
  networks: {
    ...etc...
  },
  mocha: {
    someOption: <some-value>,
    anotherOption: <another-value>
  }
};

Truffle just passes mocha this object so anything that usually works with mocha should theoretically be fine. The contract abstraction aliases describe and passes in accounts and shouldn't interfere with anything.

An example of mocha extensions working ok with truffle is eth-gas-reporter which uses mocha's third-party-reporter feature.

If you have a chance could you see if mocha-prepare can be enabled through the config?

cgewecke commented 6 years ago

@krzkaczor Do the mocha options in the config work for your use case? Is this closable?

krzkaczor commented 6 years ago

@cgewecke thanks for your response. So I guess, what I am asking about is: can i run mocha cli (node_modules/.bin/mocha) with some set of parameters and have the same outcome as running truffle test. This would be cool since it would allow more easily to integrate with IDEs (things like: run this test only).

cgewecke commented 6 years ago

@krzkaczor At the moment no - unless you write your own version of what happens at truffle in the test command. That code is mostly here and the key resources injected into the test environment are here. Mocha's also passed the config options object and accounts are injected into contract test wrapper.

It is possible to write contract.only or contract.skip for more a little more control over what tests run.

truffle test is one of next things on the schedule for a rewrite - I guess I would like to keep this open now. Agree we should try support the use case you identified, along with other test frameworks like jest etc.