/
inspect-test.js
178 lines (140 loc) · 4.51 KB
/
inspect-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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import { expect } from 'chai';
import { describe, it } from 'mocha';
import { inspect } from '../inspect';
import { invariant } from '../invariant';
describe('inspect', () => {
it('undefined', () => {
expect(inspect(undefined)).to.equal('undefined');
});
it('null', () => {
expect(inspect(null)).to.equal('null');
});
it('boolean', () => {
expect(inspect(true)).to.equal('true');
expect(inspect(false)).to.equal('false');
});
it('string', () => {
expect(inspect('')).to.equal('""');
expect(inspect('abc')).to.equal('"abc"');
expect(inspect('"')).to.equal('"\\""');
});
it('number', () => {
expect(inspect(0.0)).to.equal('0');
expect(inspect(3.14)).to.equal('3.14');
expect(inspect(NaN)).to.equal('NaN');
expect(inspect(Infinity)).to.equal('Infinity');
expect(inspect(-Infinity)).to.equal('-Infinity');
});
it('function', () => {
const unnamedFuncStr = inspect(
// istanbul ignore next (Never called and used as a placeholder)
() => invariant(false),
);
expect(unnamedFuncStr).to.equal('[function]');
// istanbul ignore next (Never called and used as a placeholder)
function namedFunc() {
invariant(false);
}
expect(inspect(namedFunc)).to.equal('[function namedFunc]');
});
it('array', () => {
expect(inspect([])).to.equal('[]');
expect(inspect([null])).to.equal('[null]');
expect(inspect([1, NaN])).to.equal('[1, NaN]');
expect(inspect([['a', 'b'], 'c'])).to.equal('[["a", "b"], "c"]');
expect(inspect([[[]]])).to.equal('[[[]]]');
expect(inspect([[['a']]])).to.equal('[[[Array]]]');
expect(inspect([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])).to.equal(
'[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]',
);
expect(inspect([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])).to.equal(
'[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ... 1 more item]',
);
expect(inspect([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])).to.equal(
'[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ... 2 more items]',
);
});
it('object', () => {
expect(inspect({})).to.equal('{}');
expect(inspect({ a: 1 })).to.equal('{ a: 1 }');
expect(inspect({ a: 1, b: 2 })).to.equal('{ a: 1, b: 2 }');
expect(inspect({ array: [null, 0] })).to.equal('{ array: [null, 0] }');
expect(inspect({ a: { b: {} } })).to.equal('{ a: { b: {} } }');
expect(inspect({ a: { b: { c: 1 } } })).to.equal('{ a: { b: [Object] } }');
const map = Object.create(null);
map.a = true;
map.b = null;
expect(inspect(map)).to.equal('{ a: true, b: null }');
});
it('use toJSON if provided', () => {
const object = {
toJSON() {
return '<json value>';
},
};
expect(inspect(object)).to.equal('<json value>');
});
it('handles toJSON that return `this` should work', () => {
const object = {
toJSON() {
return this;
},
};
expect(inspect(object)).to.equal('{ toJSON: [function toJSON] }');
});
it('handles toJSON returning object values', () => {
const object = {
toJSON() {
return { json: 'value' };
},
};
expect(inspect(object)).to.equal('{ json: "value" }');
});
it('handles toJSON function that uses this', () => {
const object = {
str: 'Hello World!',
toJSON() {
return this.str;
},
};
expect(inspect(object)).to.equal('Hello World!');
});
it('detect circular objects', () => {
const obj = {};
obj.self = obj;
obj.deepSelf = { self: obj };
expect(inspect(obj)).to.equal(
'{ self: [Circular], deepSelf: { self: [Circular] } }',
);
const array = [];
array[0] = array;
array[1] = [array];
expect(inspect(array)).to.equal('[[Circular], [[Circular]]]');
const mixed = { array: [] };
mixed.array[0] = mixed;
expect(inspect(mixed)).to.equal('{ array: [[Circular]] }');
const customA = {
toJSON: () => customB,
};
const customB = {
toJSON: () => customA,
};
expect(inspect(customA)).to.equal('[Circular]');
});
it('Use class names for the short form of an object', () => {
class Foo {
foo: string;
constructor() {
this.foo = 'bar';
}
}
expect(inspect([[new Foo()]])).to.equal('[[[Foo]]]');
(Foo.prototype: any)[Symbol.toStringTag] = 'Bar';
expect(inspect([[new Foo()]])).to.equal('[[[Bar]]]');
const objectWithoutClassName = new (function () {
// eslint-disable-next-line no-invalid-this
this.foo = 1;
})();
expect(inspect([[objectWithoutClassName]])).to.equal('[[[Object]]]');
});
});