-
Notifications
You must be signed in to change notification settings - Fork 3
/
human-error.test.js
120 lines (100 loc) · 3.35 KB
/
human-error.test.js
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
// TODO: properly test this
const errors = require('./human-error')({
url: key => `https://example.com/${key}`,
extra: {
statusCode: 500
}
});
describe('human-error', () => {
it('loads', () => {
expect(errors).not.toBe(undefined);
expect(errors).not.toBe(null);
expect(errors).not.toBe(false);
});
it('throws error if not defined', () => {
expect(() => errors.Three()).toThrow();
errors.One = () => '';
errors.Two = () => '';
expect(() => errors.Three()).toThrow();
});
it('can add an error', () => {
errors.SaveOne = () => {};
expect(errors.SaveOne instanceof Function).toBe(true);
});
it('can use an error', done => {
errors.saveAnother = () => { done(); };
errors.saveAnother();
});
it('should return an error', () => {
errors['human.test.one'] = (world = '世界') => `Hello ${world}`;
expect(errors('human.test.one') instanceof Error).toBe(true);
});
it('uses default arguments', () => {
errors['human.test.one'] = ({ type = 'a' }) => {
expect(type).toBe('a');
return '';
};
errors('human.test.one');
});
it('can be passed arguments', () => {
errors['human.test.one'] = ({ type = 'a' }) => {
expect(type).toBe('b');
return '';
};
errors('human.test.one', { type: 'b' });
});
it('sets the error to show', () => {
errors['human.test.b'] = () => 'Hello world';
let msg = errors('human.test.b').message;
expect(msg.includes('Hello world')).toBe(true);
});
it('sets the url to show', () => {
errors['human.test.one'] = () => 'Hello world';
let msg = errors('human.test.one').message;
expect(msg.includes('https://example.com/human.test.one')).toBe(true);
});
it('sets the url to show', () => {
const errors = require('./human-error')({ url: 'https://exampleb.com/' });
errors['human.test.one'] = () => 'Hello world';
let msg = errors('human.test.one').message;
expect(msg.includes('https://exampleb.com/')).toBe(true);
});
it('can generate a plain one without url', () => {
const errors = require('./human-error')({ plain: true });
errors['human.test.one'] = () => 'Hello world';
let msg = errors('human.test.one').message;
expect(msg.includes('┌')).toBe(false);
});
it('can generate a plain one with url', () => {
const errors = require('./human-error')({ url: 'https://example.com/', plain: true });
errors['human.test.one'] = () => 'Hello world';
let msg = errors('human.test.one').message;
expect(msg.includes('┌')).toBe(false);
});
});
const { line, char } = require('./human-error');
describe('helpers', () => {
it('works empty', () => {
char();
expect(char()).toBe('');
line();
expect(line()).toBe(' ');
});
it('returns a N char string', () => {
expect(char('a', 6).length).toBe(6);
expect(char('a', 6)).toBe('aaaaaa');
});
it('returns a N-2 char string', () => {
expect(line('a', 10).length).toBe(6);
expect(line('a', 10)).toBe('a ');
});
it('defaults to empty chars', () => {
expect(char(undefined, 6).length).toBe(6);
expect(char(undefined, 6)).toMatch(/^\s+$/);
expect(line()).toMatch(/^\s+$/);
});
it('keeps them whole if too large', () => {
let str = Array(100).join(' ');
expect(line(str)).toBe(str);
});
});