Sitemap

Jest: Complex Test Case - Part1

2 min readNov 10, 2021

--

Press enter or click to view image in full size

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

Jest Each

Press enter or click to view image in full size

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.

Press enter or click to view image in full size

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

Press enter or click to view image in full size
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.

Get Lee young-jun’s stories in your inbox

Join Medium for free to get updates from this writer.

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.

Press enter or click to view image in full size
Test Result for Simple Test Case with String Maps

Now Test Name is readable even with Test Case! 😀

Next? Part2

--

--