How to pass in the done() parameter on an async jest test.each case using the table as "Tagged Template Literal"

Viewed 11

This question is related to this one. But it's slightly different because I was wondering how to use the done() callback when a Tagged Template Literal is used. So it's not only using the done() callback but also combining it with the "Tagged Template Literal" (if these two concepts can be combined).

In a "regular" test I would do something like:

test(" ... ", (done) => {
    service.get(endpoint).subscribe((resp: any) => {
      expect(resp).toEqual(response)
      done()
    })
  })

To check that the get, that returns an Observable, is returning what is expected. I would like to use this approach with a table as the input and expected parameters. So something like (Warning! this is not working):

test.each<any  | jest.DoneCallback>`
  a    | b    | expected
  ${1} | ${1} | ${2}
  ${1} | ${2} | ${3}
  ${2} | ${1} | ${3}
`('returns $expected when $a is added $b', ({a, b, expected}, done: jest.DoneCallback) => {
  expect(a + b).toBe(expected);
  done();
});

The only way I found to make it works is using the "Array" approach instead the "Tagged Template Literal":

test.each<any  | jest.DoneCallback>([
    {a: 1, b: 1, expected: 2},
    {a: 1, b: 2, expected: 3},
    {a: 2, b: 1, expected: 3},
  ])('.add($a, $b)', ({a, b, expected}, done: jest.DoneCallback) => {
    expect(a + b).toBe(expected);
    done()
  });

But I would like to know whether this is possible with the Tagged Template Literal approach.

0 Answers
Related