'How can I use a string representation of an enum in a Jest test.each case?

When writing a Jest test.each() test using an enum like below

enum MyEnum {
    FirstValue,
    SecondValue,
    ThirdValue
}

describe('my example', () => {
    test.each([MyEnum.FirstValue, MyEnum.SecondValue, MyEnum.ThirdValue])(
        '%o',
        (input: MyEnum) => {
            expect(input).toBeDefined();
        }
    );
});

The output text is:

my example
  0
  1
  2

I would like it to be

my example
  FirstValue
  SecondValue
  ThirdValue

Using other formatting parameters like %p or %j like listed in the Jest docs don't change anything



Solution 1:[1]

I just thought of an answer, I could use describe.each in stead:

describe.each(testcases)('my test', (input: MyEnum) => {
    test(`${MyEnum[input]}`, () => {
        expect(input).toBeDefined();
    });
});

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Peter Simonis