kentcdodds / testing-node-apps

Test Node.js Backends on TestingJavaScript.com
https://testingjavascript.com/courses/test-node-js-backends
Other
384 stars 204 forks source link

use built-in test.each instead of jest-in-case #8

Closed p10ns11y closed 4 years ago

p10ns11y commented 4 years ago

isn't the following is more readable and better than what we have in src/utils/__tests__/auth.final.extra-3.js

test.each([
  ['a2c!', 'too short', false],
  ['123456!', 'no alphabet characters', false],
  ['ABCdef!', 'no numbers', false],
  ['abc123!', 'no uppercase letters', false],
  ['ABC123!', 'no lowercase letters', false],
  ['ABCdef123', 'no non-alphanumeric characters', false]
])('password %s is invalid for being %s', (password, reason, isValid) => {
  expect(isPasswordAllowed(password)).toBe(isValid)
})

// Or Template literal
test.each`
  password      | reason                              | isValid
  ${'a2c!'}     | ${'too short'}                      | ${false}
  ${'123456!'}  | ${'no alphabet characters'}         | ${false}
  ${'ABCdef!'}  | ${'no numbers'}                     | ${false}
  ${'abc123!'}  | ${'no uppercase letters'}           | ${false}
  ${'ABC123!'}  | ${'no lowercase letters'}           | ${false}
  ${'ABCdef123'}| ${'no non-alphanumeric characters'} | ${false}
`('password $password is invalid for being $reason', ({password, isValid}) => {
  expect(isPasswordAllowed(password)).toBe(isValid)
})
kentcdodds commented 4 years ago

You can feel free to use test.each. I personally don't feel like it's any more readable which is why I don't use test.each :)