forked from exercism/javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror-handling.spec.js
More file actions
72 lines (62 loc) · 1.96 KB
/
Copy patherror-handling.spec.js
File metadata and controls
72 lines (62 loc) · 1.96 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
import { describe, expect, test, xtest } from '@jest/globals';
import { processString } from './error-handling';
describe('Error Handling', () => {
xtest('throws TypeError if input is not a string', () => {
expect(() => processString(42)).toThrow(
expect.objectContaining({
name: 'TypeError',
message: expect.stringMatching(/.+/),
}),
);
});
xtest('returns null if string is empty', () => {
expect(processString('')).toBeNull();
});
xtest('throws error if input is too short', () => {
expect(() => processString('short')).toThrow(
expect.objectContaining({
name: 'RangeError',
message: expect.stringMatching(/.+/),
}),
);
});
xtest('throws error if input is too long', () => {
const longString = 'a'.repeat(101);
expect(() => processString(longString)).toThrow(
expect.objectContaining({
name: 'RangeError',
message: expect.stringMatching(/.+/),
}),
);
});
xtest('throws error if input contains a mix of letters and numbers', () => {
expect(() => processString('12345test6789text')).toThrow(
expect.objectContaining({
name: 'SyntaxError',
message: expect.stringMatching(/.+/),
}),
);
});
xtest('returns uppercase string if input is valid', () => {
expect(processString('hellotherefriend')).toBe('HELLOTHEREFRIEND');
});
xtest('never throws a generic Error for any invalid input', () => {
const invalidInputs = [
42, // TypeError
'short', // RangeError (too short)
'a'.repeat(101), // RangeError (too long)
'12345test6789text', // SyntaxError (mixed)
];
for (const input of invalidInputs) {
let error;
try {
processString(input);
} catch (err) {
error = err;
}
expect(error).toBeInstanceOf(Error);
expect(error.constructor).not.toBe(Error);
expect(error.message).toEqual(expect.stringMatching(/.+/));
}
});
});