Hi I’m Juha and I just hate writing unit tests. But too often I end up missing the unit tests that I didn’t write. This love-hate relationship with unit tests made be ask; Is there a way to do automated unit test writing for Typescript? There have to be a better way than manually writing everything … right, anyone?
This far I have not found anything so I’m making the tool myself.
Using end-to-end tests to unit test a function 😬
Quite often I find myself making end-to-end tests in order to “unit test” a function I just refactored. Postman for API calls or Cypress to click UI buttons and send requests from the web app. But this is time consuming to run and just does not feel right. I’m still missing the unit tests.
This made me think if I could somehow “record or something” the execution of the function I’m “unit testing” when the runtime executes the function during the end-2-end test. We have the inputs and the output is generated for us. Also the data to create mocks is available. It is then for me as a developer to see if the unit test is good.
Automated unit test writing for Typescript
I have written a library and a VSCode IDE extension using that library to record runtime execution of Typescript. The Extension is called Testent and you can get it from the VSCode marketplace.
The process works the following way:
- Code preprocessing. Adding smart “console.logs” to the code
- Running the preprocessed code and recording data from the smart console logs
- Unit test writing from the recorded data
At the moment pure functions work with primitive inputs (string, number, boolean, etc.). Next thing is to support for objects as inputs and support for testing classes.
Demo of automated unit test writing
In the below video you can see an example on how to automatically write unit tests for a normal Typescript function.
I dream of a “test template” for libraries developers use. The template would show Testent and other future libraries how to mock calls made to that library and how to handle recorded data. For this to work I’m working on an API or a way to make this template.
Testing your code should be a lot easier. Most likely in the future AI would be first analyzing user’s code and then it would generate a non-redundant and smart input dataset to test a function with.
You can also read about my colleague’s visions the future of testing moving to AI.