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

jest-in-case #16

Open xerosanyam opened 2 years ago

xerosanyam commented 2 years ago

starting jest27, instead of jest-in-case, test.each can be used because it doesn't require any extra library

ACPK commented 1 year ago

Here's an example of repetitive Jest test cases with test.each.

import {isPasswordAllowed} from '../auth'

describe('isPasswordAllowed', () => {
  const validPasswords = [['valid password', '!aBc1234']]

  const invalidPasswords = [
    ['too short', 'a2c!'],
    ['no letters', '123456!'],
    ['no numbers', 'ABCdef!'],
    ['no uppercase letters', 'abc123!'],
    ['no lowercase letters', 'ABC123!'],
    ['no non-alphanumeric characters', 'ABCdef123'],
  ]

  test.each(validPasswords)('%p - %p', (firstArgs, secondArgs) => {
    expect(isPasswordAllowed(secondArgs)).toBe(true)
  })

  test.each(invalidPasswords)('%p - %p', (firstArgs, secondArgs) => {
    expect(isPasswordAllowed(secondArgs)).toBe(false)
  })
})