forked from Lilly-Protocol/lily-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror-typeguard.test.ts
More file actions
56 lines (49 loc) · 2.05 KB
/
Copy patherror-typeguard.test.ts
File metadata and controls
56 lines (49 loc) · 2.05 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
import { describe, it, expect } from 'vitest';
import { isLilySdkError, LILY_ERROR_CODES } from '../src/errors/sdk-error';
import {
LilySdkError,
LilyConfigError,
LilyApiError,
LilyAuthenticationError,
LilyTransportError,
LilyValidationError,
} from '../src/errors/sdk-error';
describe('isLilySdkError type guard', () => {
it('returns true for LilySdkError instances', () => {
expect(isLilySdkError(new LilySdkError('test'))).toBe(true);
});
it('returns true for subclass instances', () => {
expect(isLilySdkError(new LilyConfigError('test'))).toBe(true);
expect(isLilySdkError(new LilyApiError('test'))).toBe(true);
expect(isLilySdkError(new LilyAuthenticationError('test'))).toBe(true);
expect(isLilySdkError(new LilyTransportError('test'))).toBe(true);
expect(isLilySdkError(new LilyValidationError('test'))).toBe(true);
});
it('returns false for plain Error', () => {
expect(isLilySdkError(new Error('test'))).toBe(false);
});
it('returns false for non-Error values', () => {
expect(isLilySdkError(null)).toBe(false);
expect(isLilySdkError(undefined)).toBe(false);
expect(isLilySdkError('string')).toBe(false);
expect(isLilySdkError(42)).toBe(false);
expect(isLilySdkError({ message: 'fake' })).toBe(false);
});
it('returns false for objects that look like errors but are not', () => {
const fake = { name: 'LilySdkError', message: 'fake' };
expect(isLilySdkError(fake)).toBe(false);
});
});
describe('LILY_ERROR_CODES constants', () => {
it('defines all expected error codes', () => {
expect(LILY_ERROR_CODES.CONFIG_ERROR).toBe('CONFIG_ERROR');
expect(LILY_ERROR_CODES.API_ERROR).toBe('API_ERROR');
expect(LILY_ERROR_CODES.AUTHENTICATION_ERROR).toBe('AUTHENTICATION_ERROR');
expect(LILY_ERROR_CODES.TRANSPORT_ERROR).toBe('TRANSPORT_ERROR');
expect(LILY_ERROR_CODES.VALIDATION_ERROR).toBe('VALIDATION_ERROR');
expect(LILY_ERROR_CODES.TIMEOUT).toBe('TIMEOUT');
});
it('is frozen/immutable', () => {
expect(Object.isFrozen(LILY_ERROR_CODES)).toBe(true);
});
});