One of the most crucial things you can do when writing tests is ensuring that the error message explains the problem as clearly as possible so it can be addressed quickly. Let’s improve our test by generating test titles so error messages are more descriptive.

//auth.js

function isPasswordAllowed(password) {
  return (
    password.length > 6 &&
    // non-alphanumeric
    /\W/.test(password) &&
    // digit
    /\d/.test(password) &&
    // capital letter
    /[A-Z]/.test(password) &&
    // lowercase letter
    /[a-z]/.test(password)
  )
}

 

import {isPasswordAllowed} from '../auth'

describe('isPasswordAllowed', () => {
  const allowedPwds = ['!aBc123']
  const disallowedPwds = {
    'too short': 'a2c!',
    'no alphabet characters': '123456',
    'no numbers': 'ABCdef!',
    'no uppercase letters': 'abc123!',
    'no lowercase letters': 'ABC123!',
    'no non-alphanumeric characters': 'ABCdef123',
  }
  allowedPwds.forEach((pwd) => {
    test(`allow ${pwd}`, () => {
      expect(isPasswordAllowed(pwd)).toBeTruthy()
    })
  })
  Object.entries(disallowedPwds).forEach(([key, value]) => {
    test(`disallow - ${key}: ${value}`, () => {
      expect(isPasswordAllowed(value)).toBeFalsy()
    })
  })
})

 

[Unit testing] Improve Error Messages by Generating Test Titles

相关文章:

  • 2022-01-30
  • 2021-08-06
  • 2022-12-23
  • 2022-12-23
  • 2021-08-07
  • 2022-12-23
  • 2021-11-07
  • 2021-12-09
猜你喜欢
  • 2021-12-12
  • 2022-01-02
  • 2022-01-23
  • 2022-03-02
  • 2021-09-05
  • 2021-06-18
  • 2021-10-26
相关资源
相似解决方案