summaryrefslogtreecommitdiff
path: root/deps/node/deps/npm/node_modules/err-code/test/test.js
blob: 81b2b26d37858e7e3c6474f49ba8361fb6cde0a4 (plain)
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
'use strict';

var errcode = require('../index');
var expect = require('expect.js');

describe('errcode', function () {
    describe('string as first argument', function () {
        it('should create an error object without code', function () {
            var err = errcode('my message');

            expect(err).to.be.an(Error);
            expect(err.hasOwnProperty(err.code)).to.be(false);
        });

        it('should create an error object with code', function () {
            var err = errcode('my message', 'ESOME');

            expect(err).to.be.an(Error);
            expect(err.code).to.be('ESOME');
        });

        it('should create an error object with code and properties', function () {
            var err = errcode('my message', 'ESOME', { foo: 'bar', bar: 'foo' });

            expect(err).to.be.an(Error);
            expect(err.code).to.be('ESOME');
            expect(err.foo).to.be('bar');
            expect(err.bar).to.be('foo');
        });

        it('should create an error object without code but with properties', function () {
            var err = errcode('my message', { foo: 'bar', bar: 'foo' });

            expect(err).to.be.an(Error);
            expect(err.code).to.be(undefined);
            expect(err.foo).to.be('bar');
            expect(err.bar).to.be('foo');
        });
    });

    describe('error as first argument', function () {
        it('should accept an error and do nothing', function () {
            var myErr = new Error('my message');
            var err = errcode(myErr);

            expect(err).to.be(myErr);
            expect(err.hasOwnProperty(err.code)).to.be(false);
        });

        it('should accept an error and add a code', function () {
            var myErr = new Error('my message');
            var err = errcode(myErr, 'ESOME');

            expect(err).to.be(myErr);
            expect(err.code).to.be('ESOME');
        });

        it('should accept an error object and add code & properties', function () {
            var myErr = new Error('my message');
            var err = errcode(myErr, 'ESOME', { foo: 'bar', bar: 'foo' });

            expect(err).to.be.an(Error);
            expect(err.code).to.be('ESOME');
            expect(err.foo).to.be('bar');
            expect(err.bar).to.be('foo');
        });

        it('should create an error object without code but with properties', function () {
            var myErr = new Error('my message');
            var err = errcode(myErr, { foo: 'bar', bar: 'foo' });

            expect(err).to.be.an(Error);
            expect(err.code).to.be(undefined);
            expect(err.foo).to.be('bar');
            expect(err.bar).to.be('foo');
        });
    });

    it('should allow passing null & undefined in the first argument', function () {
        var err;

        err = errcode(null, 'ESOME');
        expect(err).to.be.an(Error);
        expect(err.message).to.be('null');
        expect(err.code).to.be('ESOME');

        err = errcode(undefined, 'ESOME');
        expect(err).to.be.an(Error);
        expect(err.message).to.be('');
        expect(err.code).to.be('ESOME');
    });
});