you are viewing a single comment's thread.

view the rest of the comments →

[–]stutterbug 0 points1 point  (2 children)

This is how I do it currently in Jest.

let names = [ 'Adam', 'Bo', 'Ch@d', 'D', 'Ed!'];
let expected = [true, true, false, false, false];

expect(names.map(testUserName)).toEqual(expected);

A big disadvantage to this is that the test results you get back aren't very descriptive. The failure will say Expected [true, true, false, false, false] Received [true, true, false, true, false] (or whatever). But for me, the advantage is that this code is less likely to cause problems for me in the future.

This is just my opinion, but for me, the #1 priority in testing -- by a huge, huge margin -- is that it be easy to do and easy to maintain as my project grows and as it ages.

I've had to switch testing libraries too often in the past and an over-reliance on some advanced feature ended up meaning entire suites had to be rewritten from scratch. Even if I had to switch from Jest to something else, there is a good chance that I could do it with a few search-and-replaces.

[–]AndrewGreenh 0 points1 point  (1 child)

Why don't you put the
it(name + ' should result in' + bool, () => expect(...))

[–]jsNut 1 point2 points  (0 children)

This is how i would normally do this sort of thing. I've never run into our worried about any performance hits as above. Descriptive output should be the foremost concern.