password-strength.test.ts 1.4 KB

12345678910111213141516171819202122232425262728293031323334
  1. import { describe, expect, it } from 'vitest'
  2. import { passwordStrength } from './password-strength'
  3. describe('passwordStrength', () => {
  4. it('returns empty values for message, warning and strength for empty input', async () => {
  5. const result = await passwordStrength('')
  6. expect(result).toEqual({ message: '', warning: '', strength: 0 })
  7. })
  8. it('returns max length message, warning, and strength 0 for password longer than 99 characters', async () => {
  9. const longPassword = 'a'.repeat(100)
  10. const result = await passwordStrength(longPassword)
  11. expect(result.message).toMatch(/maximum length/i)
  12. expect(result.warning).toMatch(/less than 100 characters/i)
  13. expect(result.strength).toBe(0)
  14. })
  15. it('returns strong score, suggestion, and empty warning for strong password', async () => {
  16. const result = await passwordStrength('ActuallyAStrongPassword123!')
  17. expect(result.message).toMatch(/strong/i)
  18. expect(result.message).toContain('This password is strong')
  19. expect(result.warning).toBe('')
  20. expect(result.strength).toBe(4)
  21. })
  22. it('returns weak score, suggestion, and warning for weak password', async () => {
  23. const result = await passwordStrength('weak')
  24. expect(result.message).toMatch(/not secure/i)
  25. expect(result.message).toContain('This password is not secure enough')
  26. expect(result.warning).toMatch(/you need a stronger password/i)
  27. expect(result.strength).toBe(1)
  28. })
  29. })