How to print the actual elements of an array in the title of the test suite?

Viewed 12

I am trying to run a test for a program which merges a given interval into an array of intervals. I want the test to print the values of the given array of intervals and the interval to be inserted. But [Array, Array] gets printed instead of the values of the array.

What I want:

  Insert Interval:
    intervals=[[1, 3], [6, 9]],
    newInterval=[2, 5],
    expected=[[1, 5], [6, 9]]
    ✓ returns intervals after inserting given interval (4 ms)

What actually happens: (notice [Array] instead of [1, 3])

  Insert Interval:
    intervals=[[Array], [Array]],
    newInterval=[2, 5],
    expected=[[Array], [Array]]
    ✓ returns intervals after inserting given interval (4 ms)

What I tried:

import { insert } from './insert';

const knownTests = [
  [[[1, 3], [6, 9]], [2, 5], [[1, 5], [6, 9]]],
  [[[1, 2], [3, 5], [6, 7], [8, 10], [12, 16]], [4, 8], [[1, 2], [3, 10], [12, 16]]],
];

const prepareTests = knownTests.map(array => ({
  intervals: array[0],
  newInterval: array[1],
  expected: array[2],
})
);

describe.each(prepareTests)(
  `Insert Interval:
    intervals=$intervals,
    newInterval=$newInterval,
    expected=$expected`,
  ({ intervals, newInterval, expected }) => {
    test(
      `returns intervals after inserting given interval`, () => {
        expect(insert(intervals, newInterval)).toEqual(expected);
      }
    )
  }
);
0 Answers
Related