zoukankan      html  css  js  c++  java
  • [Unit testing] Improve Error Messages by Generating Test Titles

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

  • 相关阅读:
    喜欢的事,用心去做!
    初识mysql
    使用连发互联空间+SQLyog 设置我们的数据库链接
    checkbox实现全选全不选
    各种电脑进入BIOS快捷键
    Fetching data with Ajax小例子
    JS 变量类型互相转换
    XMLHTTPRequestObject获取服务器数据
    js事件
    关闭华硕笔记本触摸屏
  • 原文地址:https://www.cnblogs.com/Answer1215/p/15104821.html
Copyright © 2011-2022 走看看