-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patherrors.spec.ts
More file actions
177 lines (141 loc) · 5.28 KB
/
errors.spec.ts
File metadata and controls
177 lines (141 loc) · 5.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import {
AuthKitError,
SessionEncryptionError,
TokenValidationError,
TokenRefreshError,
} from './errors.js';
describe('AuthKitError', () => {
it('creates error with message', () => {
const error = new AuthKitError('Test error');
expect(error.message).toBe('Test error');
expect(error.name).toBe('AuthKitError');
expect(error).toBeInstanceOf(Error);
});
it('creates error with cause', () => {
const originalError = new Error('Original error');
const error = new AuthKitError('Test error', originalError);
expect(error.cause).toBe(originalError);
});
it('creates error with data', () => {
const data = { userId: '123', action: 'login' };
const error = new AuthKitError('Test error', undefined, data);
expect(error.data).toEqual(data);
});
it('creates error with cause and data', () => {
const originalError = new Error('Original error');
const data = { userId: '123' };
const error = new AuthKitError('Test error', originalError, data);
expect(error.cause).toBe(originalError);
expect(error.data).toEqual(data);
});
});
describe('SessionEncryptionError', () => {
it('creates error with correct name', () => {
const error = new SessionEncryptionError('Encryption failed');
expect(error.name).toBe('SessionEncryptionError');
expect(error.message).toBe('Encryption failed');
expect(error).toBeInstanceOf(AuthKitError);
expect(error).toBeInstanceOf(Error);
});
it('creates error with cause', () => {
const originalError = new Error('Crypto error');
const error = new SessionEncryptionError(
'Encryption failed',
originalError,
);
expect(error.cause).toBe(originalError);
});
});
describe('TokenValidationError', () => {
it('creates error with correct name', () => {
const error = new TokenValidationError('Token invalid');
expect(error.name).toBe('TokenValidationError');
expect(error.message).toBe('Token invalid');
expect(error).toBeInstanceOf(AuthKitError);
expect(error).toBeInstanceOf(Error);
});
it('creates error with cause', () => {
const originalError = new Error('JWT malformed');
const error = new TokenValidationError('Token invalid', originalError);
expect(error.cause).toBe(originalError);
});
});
describe('TokenRefreshError', () => {
it('creates error with correct name', () => {
const error = new TokenRefreshError('Refresh failed');
expect(error.name).toBe('TokenRefreshError');
expect(error.message).toBe('Refresh failed');
expect(error).toBeInstanceOf(AuthKitError);
expect(error).toBeInstanceOf(Error);
});
it('creates error with cause', () => {
const originalError = new Error('Network error');
const error = new TokenRefreshError('Refresh failed', originalError);
expect(error.cause).toBe(originalError);
});
it('creates error with userId and sessionId', () => {
const error = new TokenRefreshError('Refresh failed', undefined, {
userId: 'user_123',
sessionId: 'session_456',
});
expect(error.userId).toBe('user_123');
expect(error.sessionId).toBe('session_456');
});
it('creates error with cause and context', () => {
const originalError = new Error('Network error');
const error = new TokenRefreshError('Refresh failed', originalError, {
userId: 'user_123',
sessionId: 'session_456',
});
expect(error.cause).toBe(originalError);
expect(error.userId).toBe('user_123');
expect(error.sessionId).toBe('session_456');
});
it('creates error with partial context (userId only)', () => {
const error = new TokenRefreshError('Refresh failed', undefined, {
userId: 'user_123',
});
expect(error.userId).toBe('user_123');
expect(error.sessionId).toBeUndefined();
});
it('creates error with partial context (sessionId only)', () => {
const error = new TokenRefreshError('Refresh failed', undefined, {
sessionId: 'session_456',
});
expect(error.userId).toBeUndefined();
expect(error.sessionId).toBe('session_456');
});
it('has undefined properties when no context provided', () => {
const error = new TokenRefreshError('Refresh failed');
expect(error.userId).toBeUndefined();
expect(error.sessionId).toBeUndefined();
});
});
describe('error inheritance', () => {
it('maintains proper inheritance chain', () => {
const sessionError = new SessionEncryptionError('test');
const tokenError = new TokenValidationError('test');
const refreshError = new TokenRefreshError('test');
expect(sessionError).toBeInstanceOf(SessionEncryptionError);
expect(sessionError).toBeInstanceOf(AuthKitError);
expect(sessionError).toBeInstanceOf(Error);
expect(tokenError).toBeInstanceOf(TokenValidationError);
expect(tokenError).toBeInstanceOf(AuthKitError);
expect(tokenError).toBeInstanceOf(Error);
expect(refreshError).toBeInstanceOf(TokenRefreshError);
expect(refreshError).toBeInstanceOf(AuthKitError);
expect(refreshError).toBeInstanceOf(Error);
});
it('can be caught as AuthKitError', () => {
const errors = [
new SessionEncryptionError('test'),
new TokenValidationError('test'),
new TokenRefreshError('test'),
];
errors.forEach(error => {
expect(() => {
throw error;
}).toThrow(AuthKitError);
});
});
});