testing-node-apps icon indicating copy to clipboard operation
testing-node-apps copied to clipboard

jest-in-case

Open xerosanyam opened this issue 3 years ago • 1 comments

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

xerosanyam avatar Dec 27 '21 13:12 xerosanyam

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)
  })
})

ACPK avatar Jul 10 '23 19:07 ACPK