Related #1458
A simple fix is to add one line at https://github.com/trufflesuite/truffle/blob/8013923d4a25585981d86858f4d979ac1f001fb4/packages/truffle-core/lib/test.js#L237
Use chai directly in any test.
Global identifier chai must be accessible.
Only chai.expect and chai.assert are exported.
truffle version): Truffle v5.0.21 (core: 5.0.21)node --version): N/Anpm --version): N/AAfter some discussion, I think the better approach is for users to require("chai") manually. Putting it in the global space feels like scope creep. We'd like to remove globals from truffle test, ultimately.
Closing this, but feel free to offer a counter-argument. (I don't feel too strongly). Anyway, thanks for raising this!
@gnidan It seems @schuma7 and me could not emphasis on the problem: Truffle bundles own chai copy and it is NOT importable through require('chai'). So, it's not possible to install custom plugins.
Error: Cannot find module 'chai'
at Function.Module._resolveFilename (module.js:548:15)
at Function.Module._load (module.js:475:25)
at Module.require (module.js:597:17)
at require (internal/module.js:11:18)
Thanks for that info @andvgal . Reopening since it sounds like we should proceed with removing the injected chai globals.
@CruzMolina as Truffle uses a bit own contract() API for test declaration, I doubt it's possible to just remove injected chai. Also, it would break a lot of projects in field.
As there is zero benefit of locally installed chai import in current tests, I believe there will be no conflict. So, injecting this global won't break the majority of projects. So, it can be a safe and quick solution.
@andvgal I could import it into my test files just fine. I just installed it with npm and added it to the dependencies section in the package.json. Afterwards I was able to require it in my js tests. Can you tell me exactly what you are doing when you get the module not found error so I can recreate your situation?
@eggplantzzz I am not installing chai separately as the one used by Truffle is not getting exposed. If you install a separate chai instance, you need to override assert and except globals to support custom plugins.
Of course, it is a solution, but it would just break all use cases in field. Based on the semver requirements, such change should not happen neither for patch nor for minor releases.
In short, I support your idea of getting rid of embedded chai, but development culture requires to postpone that to Truffle 6.x
I support that. Example for why:
Since truffle switched to the more promise based web3 1.0, it's often useful to add chai-as-promised in order to more elegantly handle promise returning methods in tests.
Setting that up however requires access to the global chai instance (example). Because of that, many truffle projects bundle their own chai. That's something I don't feel comfortable to do, because it's not clear to me if/which issues may or may not arise by shadowing the pre-injected chai instance like that.
Originally I wanted to propose including chai-as-promised in the pre-injected chai. Maybe that's something which could already be done in truffle v5 without breaking anything.
However after finding this issue, I now believe that a cleaner solution would be to either give access to the global chai object or remove it from truffle altogether.
If there is anything actionable on this subject for version 6, then we'll have to have a discussion and talk about specifications for the work.
I'm done... I was trying to use chai-bn plugin to make my life easier, but realized that it is impossible using truffle. I'll do the same as OpenZeppelin, throw truffle into the garbage and use openzeppelin-test-environment for unit testing.
Funny, but I stumbled here because of that same issue Lohan.
Most helpful comment
I'm done... I was trying to use chai-bn plugin to make my life easier, but realized that it is impossible using truffle. I'll do the same as OpenZeppelin, throw truffle into the garbage and use openzeppelin-test-environment for unit testing.