Using TypeScript for writing smart contract tests
Discussion started by @Luiserebii: https://github.com/OpenZeppelin/openzeppelin-contracts/issues/1902
Most importantly, I was curious to know if the team has thought about this before? I looked into the issues, and no mention of this was made, so this made me very curious to ask.
@nventuro: We’re actually planning on bringing some changes into the test suite! What sort of things do you think TypeScript could help with? I know there were some efforts to create typed contract objects, but I’m not sure how far along those have come. Do you use TS in your own setup?
@Luiserebii: That’s great to hear! I’m happy to hear that this is topic conveniently relevant. I think that the type-checking TypeScript could be able to offer could strengthen tests and detect bugs at compile-time. I’ve heard from friends that web3.js can sometimes return objects they weren’t quite expecting, though I can’t remember for myself if this was ever the case for me.
ESLint itself helps quite a bit in detecting things such as unused variables, I’m not sure if there’s anything TypeScript covers that ESLint doesn’t cover. Something nice about TypeScript is the ability to define interfaces for objects, thus guaranteeing that the objects you’re expecting do have the properties you’re looking for. During run-time, this issue may pass silently and simply reveal itself as
When it comes to objects with fluid structures, though, like results returned from
callfunctions, I’m not 100% certain what the best practice is for handling this in TypeScript, I think that probably using
anyas the type would work as a work-around.
I actually haven’t heard of the typed contract objects thing, but I have recently tried integrating TypeScript into a setup as a proof-of-concept, and also something reusable for anyone looking to set something like this up in the future, since it took me some research to quite get everything together. The project is called Maximum Solidity Environment, and can be found here: https://github.com/Luiserebii/Maximum-Solidity-Environment
Feel free to add your thoughts to the discussion in the forum.