Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
October 17, 2021 07:50 pm GMT

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


Original Link: https://dev.to/egm0121/write-less-test-faster-with-jest-test-gen-3lm0

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To