0

I'm a long time tape.js user and I'm working on learning how to work jest. I'm interested in providing descriptions for each my test cases as part of the assertion, ala this tape test

function myCoolTest(t) {
  t.equal('batman'.length, 6, 'batman should have the right number of characters in it');
  t.ok(1 === 1, 'basic truths should stay true');
  t.deepEqual({test: 1}, {test: 1}, 'deep equality of objects works sensibly');
  t.end();
}

I like being able to annotate my tests (eg 'batman should have the right number of characters in it'), that way as I'm reading the output it's clear whats passed and what's failed. As far as I can tell the jest equivalent is

test('example test', () => {
  expect('batman'.length).toBe(6);
  expect(1 === 1).toBeTruthy();
  expect({test: 1}).toBe({test: 1});
});

Which totally lacks description found in the first? While that's okay for simple examples like ^. The examples i've seen other places seem to suggest that if I want description I should add comments next to the relevant test, but this seems to prevent creating utility tests, eg

  const expectEqual = (a: string, b: string): void =>
    expect(JSON.parse(a)).toEqual(JSON.parse(b));

Am i just out of luck or are there methods that I am missing?

skyboyer
  • 22,209
  • 7
  • 57
  • 64
mcnutt
  • 663
  • 8
  • 12
  • 1
    Jest does not by default provide this functionality; you can optionally define your own matchers with their own messaging as laid out in [this answer](https://stackoverflow.com/a/45375363/1810460), or you could look into an additional library like [jest-expect-message](https://www.npmjs.com/package/jest-expect-message) – Hamms Feb 06 '20 at 22:38
  • That link answers my question! Thanks so much for pointing me to it! – mcnutt Feb 07 '20 at 22:17

0 Answers0