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