📜 ⬆️ ⬇️

Introscope: well, very lazy unit testing

We at work write a lot of frequently changing business logic on JS (a startup). Even too much. And all this would be desirable because of the old habit of testing, but somehow quickly, so that tests do not particularly interfere with govnododit, when it is needed, and do not tailor the module structure especially strongly for tests. At some point, the thought came that one could also automate it somehow with the help of a simple babel plug-in, so that it would first turn all private variables and methods in the test environment, and in production it would not touch. It turned out to be quite simple and fascinating. This is how Introscope appeared, with which we are now testing everything that is more or less complex.


Having handed over the project, I was distracted for several months to improve the deployment and monitoring, I rested, read Habr, and when it took to test something again, I was directly delighted with my own tool! It does not often happen to me. More precisely, never had before. So, I think that thing works. I hope you also come in handy.


In short, what can it do?


  1. Wraps the entire module in the factory function so that you can create as many new modules as you need during testing. This eliminates the need to do unmock.
  2. Since the module is wrapped, the tool knows exactly which variables the module imports and can generate mocks by their type. It works very well for a more or less functional style.
  3. It knows how to log calls and arguments to functions from external modules and put it all into a log, which is then beautifully serialized for snapshots (if you use Jest, then everything will immediately get started).
  4. Killer feature: can save the result of a call to an external function and use it with further testing. It is very convenient when testing requests to external services that we used to drive into moki.
  5. In combination with Jest, the tests are updated by clicking on one (I'm not joking) button U, which updates snapshots. Golden test approach in all its glory.
  6. Works in production for almost a year.

As a result


Tests began to be written by people who didn’t even want to hear about them before, because you only need to call a function with the necessary parameters and save a log of what happened in this function with one button. I really wanted to test something else ...


Reference: Introscope (there are still gifs screenshots!). I would be very happy constructive criticism and references to the same, but the done is much better: D


image



Source: https://habr.com/ru/post/437104/