Jest: Complex Test Case - Part1

I met complex case this time.
So I researched about how to handle complex tests more short and simple

Jest Each

Jest provides ‘each’ function to handle many test cases in once.
And I thought it is possible to make tests simpler

Test Case

First, I defined Type for Test Case

Test Case Type

Then, I appended Test Case Data.

Last, I wrote first test code like this.

Test was running successfully.
However test name was different to my expectation. :(
I wanted to print property value instead of object

Serialized Test Case

To print test name I want,
First, I created a new Type and renamed the original.

Second, I exported transformed Test Case.
To conform ‘each’, I had to transform my Test Case to Array

Finally, I modified my test code like below

What is changed?
testCase is changed to properties.
If we want to print property values at Test Name, we should provide each property as argument.

(testCase) → (passState,

And test result was I wanted like this

Test Result with Serialized Test Case

However that was little readable for me.

Custom Test Function

Some tests are use only pass state.
So I tried to make more readable test with Simple Test Case.

I appended Simple State Test Case

Case Property is only one.

With above materials I created new function for testing

I defined string maps(PassStateStrings, PassValidationStateStrings) to make state values readable.

Test Result for Simple Test Case with String Maps

Now Test Name is readable even with Test Case! 😀




Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

ReactJS vs NextJS, which is superior?

Integrating a front and back end

Simplest React and Phoenix Setup

Building a Proxy Server with Nodejs

JS scope: static, dynamic, and runtime-augmented

Optimize your Angular App using Web Workers

Some Important Javascript Topics

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Lee young-jun

Lee young-jun


More from Medium

Api Testing with Hoppscotch

Interface Hero: Jon del Strother and CoverFlow

Using custom response object while building SpringBoot API

How to mock dateTime in Flutter