DEV Community

Cover image for Write less, test faster with jest-test-gen
Giulio Dellorbo
Giulio Dellorbo

Posted on • Edited on

Write less, test faster with jest-test-gen

Hello everyone šŸ‘‹

First time posting here in the dev.to community!

What is jest-test-gen

I wanted to introduce a small cli utility jest-test-gen that I wrote to speed up writing Jest unit tests.

Every time I start writing a new test file for my code some initial boilerplate is always required:

  • import all the exports from the source file to be tested
  • import the external dependencies used in my source file
  • setup jest mocks for the dependencies used in the source code
  • create tests blocks for every exported function / class method in my source file.

I wanted to automate part of this process by parsing the source file and generating an initial test file containing the initial scaffolding, this way I can quickly start writing the actual logic for my tests.

How to use it

To generate a test file just install the tool

npm i -g jest-test-gen

and invoke it passing the path to your source file as the first argument:

jest-test-gen ~/demoproject/src/services/myServiceClass.js

The tool will generate a file named myServiceClass.generated.test.js in the same folder.

Try it out and give it a like here or ā­ļø the repo if you find it useful.

You can find the repo here: jest-test-gen on Github

Top comments (1)

Collapse
 
klvenky profile image
Venkatesh KL

I think you should try out ponicode. It's far more useful as it tries to predict the outcomes with AI assisted code generation