-
Notifications
You must be signed in to change notification settings - Fork 940
/
test.js
140 lines (114 loc) · 3.79 KB
/
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/* eslint-env mocha */
const assert = require('assert');
const debug = require('./src');
describe('debug', () => {
it('passes a basic sanity check', () => {
const log = debug('test');
log.enabled = true;
log.log = () => {};
assert.doesNotThrow(() => log('hello world'));
});
it('allows namespaces to be a non-string value', () => {
const log = debug('test');
log.enabled = true;
log.log = () => {};
assert.doesNotThrow(() => debug.enable(true));
});
it('honors global debug namespace enable calls', () => {
assert.deepStrictEqual(debug('test:12345').enabled, false);
assert.deepStrictEqual(debug('test:67890').enabled, false);
debug.enable('test:12345');
assert.deepStrictEqual(debug('test:12345').enabled, true);
assert.deepStrictEqual(debug('test:67890').enabled, false);
});
it('uses custom log function', () => {
const log = debug('test');
log.enabled = true;
const messages = [];
log.log = (...args) => messages.push(args);
log('using custom log function');
log('using custom log function again');
log('%O', 12345);
assert.deepStrictEqual(messages.length, 3);
});
describe('extend namespace', () => {
it('should extend namespace', () => {
const log = debug('foo');
log.enabled = true;
log.log = () => {};
const logBar = log.extend('bar');
assert.deepStrictEqual(logBar.namespace, 'foo:bar');
});
it('should extend namespace with custom delimiter', () => {
const log = debug('foo');
log.enabled = true;
log.log = () => {};
const logBar = log.extend('bar', '--');
assert.deepStrictEqual(logBar.namespace, 'foo--bar');
});
it('should extend namespace with empty delimiter', () => {
const log = debug('foo');
log.enabled = true;
log.log = () => {};
const logBar = log.extend('bar', '');
assert.deepStrictEqual(logBar.namespace, 'foobar');
});
it('should keep the log function between extensions', () => {
const log = debug('foo');
log.log = () => {};
const logBar = log.extend('bar');
assert.deepStrictEqual(log.log, logBar.log);
});
});
describe('rebuild namespaces string (disable)', () => {
it('handle names, skips, and wildcards', () => {
debug.enable('test,abc*,-abc');
const namespaces = debug.disable();
assert.deepStrictEqual(namespaces, 'test,abc*,-abc');
});
it('handles empty', () => {
debug.enable('');
const namespaces = debug.disable();
assert.deepStrictEqual(namespaces, '');
assert.deepStrictEqual(debug.names, []);
assert.deepStrictEqual(debug.skips, []);
});
it('handles all', () => {
debug.enable('*');
const namespaces = debug.disable();
assert.deepStrictEqual(namespaces, '*');
});
it('handles skip all', () => {
debug.enable('-*');
const namespaces = debug.disable();
assert.deepStrictEqual(namespaces, '-*');
});
it('names+skips same with new string', () => {
debug.enable('test,abc*,-abc');
const oldNames = [...debug.names];
const oldSkips = [...debug.skips];
const namespaces = debug.disable();
assert.deepStrictEqual(namespaces, 'test,abc*,-abc');
debug.enable(namespaces);
assert.deepStrictEqual(oldNames.map(String), debug.names.map(String));
assert.deepStrictEqual(oldSkips.map(String), debug.skips.map(String));
});
it('handles re-enabling existing instances', () => {
debug.disable('*');
const inst = debug('foo');
const messages = [];
inst.log = msg => messages.push(msg.replace(/^[^@]*@([^@]+)@.*$/, '$1'));
inst('@test@');
assert.deepStrictEqual(messages, []);
debug.enable('foo');
assert.deepStrictEqual(messages, []);
inst('@test2@');
assert.deepStrictEqual(messages, ['test2']);
inst('@test3@');
assert.deepStrictEqual(messages, ['test2', 'test3']);
debug.disable('*');
inst('@test4@');
assert.deepStrictEqual(messages, ['test2', 'test3']);
});
});
});