1 | const ErrorFactory = require('./index.js');
|
2 |
|
3 | describe('server/error', () => {
|
4 | it('is a function', () => {
|
5 | expect(ErrorFactory instanceof Function).toBe(true);
|
6 | });
|
7 |
|
8 | it('canNOT do simple errors', () => {
|
9 | expect(new ErrorFactory('Hello world').message).not.toBe('Hello world');
|
10 | expect(new ErrorFactory('Hello world') instanceof Error).not.toBe(true);
|
11 | });
|
12 |
|
13 | it('does not create a plain error', () => {
|
14 | expect(ErrorFactory().message).toBe(undefined);
|
15 | expect(ErrorFactory() instanceof Function).toBe(true);
|
16 | expect(ErrorFactory('Hello world').message).toBe(undefined);
|
17 | expect(ErrorFactory('Hello world') instanceof Function).toBe(true);
|
18 | expect(ErrorFactory('Hello world', {}).message).toBe(undefined);
|
19 | expect(ErrorFactory('Hello world', {}) instanceof Function).toBe(true);
|
20 | });
|
21 |
|
22 | it('can create errors from within the factory', () => {
|
23 | expect(ErrorFactory('/server/')('test') instanceof Error).toBe(true);
|
24 | expect(ErrorFactory('/server/')('test').code).toBe('/server/test');
|
25 | expect(ErrorFactory('/server/')('test', { status: 500 }).status).toBe(500);
|
26 | });
|
27 |
|
28 | it('can create errors from within the factory', () => {
|
29 | expect(ErrorFactory('/server/')('test') instanceof Error).toBe(true);
|
30 | expect(ErrorFactory('/server/')('test').code).toBe('/server/test');
|
31 | expect(ErrorFactory('/server/')('test').namespace).toBe('/server/');
|
32 | expect(ErrorFactory('/server/')('test', { status: 500 }).status).toBe(500);
|
33 | });
|
34 |
|
35 | describe('Namespaces', () => {
|
36 | const TestError = ErrorFactory('/server/', { status: 500 });
|
37 |
|
38 | it('has the correct defaults', () => {
|
39 | expect(TestError().status).toBe(500);
|
40 | expect(TestError().code).toBe('/server');
|
41 | expect(TestError().id).toBe('server');
|
42 | });
|
43 |
|
44 | it('can extend the errors', () => {
|
45 | const err = TestError('demo', { status: 501 });
|
46 | expect(err.status).toBe(501);
|
47 | expect(err.code).toBe('/server/demo');
|
48 | expect(err.id).toBe('server-demo');
|
49 | });
|
50 |
|
51 | it('is the same as with the instance', () => {
|
52 | const err = TestError('demo', { status: 501 });
|
53 | const err2 = new TestError('demo', { status: 501 });
|
54 | expect(err).toMatchObject(err2);
|
55 | });
|
56 | });
|
57 |
|
58 | describe('Define errors', () => {
|
59 | const TestError = ErrorFactory('/server/', { status: 500 });
|
60 | TestError.aaa = 'First error';
|
61 |
|
62 | it('has the correct message', () => {
|
63 | expect(TestError('aaa').message).toBe('First error');
|
64 | });
|
65 |
|
66 | it('can define an error with a function', () => {
|
67 | TestError.bbb = () => `Function error`;
|
68 | expect(TestError('bbb').message).toBe('Function error');
|
69 | });
|
70 |
|
71 | it('defines errors globally', () => {
|
72 | expect(TestError('bbb').message).toBe('Function error');
|
73 | });
|
74 |
|
75 | it('errors are namespaced', () => {
|
76 | const TestError = ErrorFactory('/server/');
|
77 | expect(TestError('bbb').message).toBe(undefined);
|
78 | });
|
79 |
|
80 | it('gets the options in the interpolation', () => {
|
81 | TestError.ccc = ({ status }) => `Function error ${status}`;
|
82 | expect(TestError('ccc', { status: 505 }).message).toBe('Function error 505');
|
83 | });
|
84 | });
|
85 | });
|