We write a lot of frequently changing business logic in JS at work (a startup). Even too much. And we want to test it all out of old habits, but fast, so that tests don’t interfere with crap-coding when we need it and don’t make the structure of modules especially strict. At some point I got the idea that I could also automate it somehow with a simple babel-plugin, so that first it reversed all the private variables and methods in the test environment, but did not touch it in production. This turned out to be quite simple and fascinating. That’s how the Introscope with which we now test everything more or less complicated.
After handing in the project, I took a few months off to improve my deplotting and monitoring, rested, read Habr, and when I needed to test something again, I fell right in love with my own toolkit! This does not happen to me very often. Or rather, it has never happened before. So, I think it works. I hope you can use it too.
Briefly, what can it do?
- Wraps the entire module in a factory function, so you can create as many new modules during testing as you want. This eliminates the need to do an unmock.
- Since the module is wrapped, the toolkit knows exactly what variables the module imports and can generate mocks based on their type. This works very well for more or less functional styles.
- Able to log calls and arguments to functions from external modules and put it all into a log, which is then serialized nicely for snapshots (if you use Jest, everything will start right away).
- Killer feature : can save the result of an external function call and use it when testing later. Very handy when testing queries to external services, which we used to manually type into mocs.
- In combination with Jest, the tests are updated by pressing one (I’m not kidding) U button, which updates the snapshots. Golden test approach in all its glory.
- Been in production for almost a year.
As a result of
The tests were written by people who didn’t even want to hear about them before, all they needed was to call a function with the right parameters and save a log of what was happening in that function with one button. I really wanted to test something else…
Link : Introscope (there are more screencast gifs in there!). I’ll be very happy to get some constructive criticism and links to the same, but done much better 😀