Using TypeScript for writing smart contract tests
Discussion started by @Luiserebii: https://github.com/OpenZeppelin/openzeppelin-contracts/issues/1902
Idea
This has been something on my mind for a while now, the potential for using TypeScript for writing tests. Strict typing could be a good boost to ensuring things are running as expected, and an argument that comes to mind is that vanilla JavaScript is a forgiving language that may not be the best for writing smart contract tests for, given how sensitive they may be.
Counter-arguments
To play devil's advocate, what comes to mind is that it can limit developer contributions since most people are familiar with JavaScript, and perhaps well-written JavaScript along with a linter might be the most productive route to go, since I've personally had issues with figuring out how to work with untyped modules I may be using.
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
undefined
.When it comes to objects with fluid structures, though, like results returned from
call
functions, I'm not 100% certain what the best practice is for handling this in TypeScript, I think that probably usingany
as 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.