Jest: Complex Test Case - Part1

Lee young-jun
2 min readNov 10, 2021

--

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,
passType,
hasRemainCount,
hasRemainTime,
isUsing,
isTransferable,
isExpired,
expected)

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! 😀

Next? Part2

--

--

No responses yet