-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
test.js
332 lines (279 loc) · 9.8 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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
import path from 'path';
import test from 'ava';
import StackUtils from '../';
import CaptureFixture from './fixtures/capture-fixture';
import {join, fixtureDir} from './_utils';
const LinuxStack1 = join(linuxStack1(), internalStack());
const WindowsStack1 = join(windowsStack1(), internalStack());
const version = process.version.slice(1).split('.').map(function (val) {
return parseInt(val, 10);
});
test('must be called with new', t => {
t.is(typeof StackUtils, 'function');
const stackUtils = StackUtils;
t.throws(() => stackUtils());
});
test('clean: truncates cwd', t => {
const expected = join([
'foo (foo.js:3:8)',
'bar (foo.js:7:2)',
'bar (bar.js:4:2)',
'Object.<anonymous> (bar.js:7:1)',
'Module._compile (module.js:398:26)',
'Object.Module._extensions..js (module.js:405:10)',
'Module.load (module.js:344:32)',
'Function.Module._load (module.js:301:12)',
'Function.Module.runMain (module.js:430:10)',
'startup (node.js:141:18)'
]);
let stack = new StackUtils({cwd: '/user/dev/project'});
t.is(stack.clean(LinuxStack1), expected, 'accepts a linux string');
t.is(stack.clean(LinuxStack1.split('\n')), expected, 'accepts an array');
t.is(stack.clean(LinuxStack1.split('\n').slice(1)), expected, 'slices off the message line');
stack = new StackUtils({cwd: 'Z:\\user\\dev\\project'});
t.is(stack.clean(WindowsStack1), expected, 'accepts a windows string');
});
test('clean: eliminates internals', t => {
let stack = new StackUtils({cwd: '/user/dev', internals: StackUtils.nodeInternals()});
var expected = join([
'foo (project/foo.js:3:8)',
'bar (project/foo.js:7:2)',
'bar (project/bar.js:4:2)',
'Object.<anonymous> (project/bar.js:7:1)'
]);
t.is(stack.clean(LinuxStack1), expected);
stack = new StackUtils({cwd: 'Z:\\user\\dev', internals: StackUtils.nodeInternals()});
t.is(stack.clean(WindowsStack1), expected);
});
test('clean: returns null if it is all internals', t => {
const stack = new StackUtils({internals: StackUtils.nodeInternals()});
t.is(stack.clean(join(internalStack())), null);
});
test('captureString: two redirects', t => {
const stack = new StackUtils({internals: internals(), cwd: fixtureDir});
const capture = new CaptureFixture(stack);
const capturedString = capture.redirect1('redirect2', 'call', 'captureString');
t.is(capturedString, join([
'CaptureFixture.call (capture-fixture.js:23:28)',
'CaptureFixture.redirect2 (capture-fixture.js:17:22)',
'CaptureFixture.redirect1 (capture-fixture.js:11:22)'
]));
});
test('captureString: with startStack function', t => {
const stack = new StackUtils({internals: internals(), cwd: fixtureDir});
const capture = new CaptureFixture(stack);
const capturedString = capture.redirect1('redirect2', 'call', 'captureString', capture.call);
t.is(capturedString, join([
'CaptureFixture.redirect2 (capture-fixture.js:17:22)',
'CaptureFixture.redirect1 (capture-fixture.js:11:22)'
]));
});
test('captureString: with limit', t => {
const stack = new StackUtils({internals: internals(), cwd: fixtureDir});
const capture = new CaptureFixture(stack);
const capturedString = capture.redirect1('redirect2', 'call', 'captureString', 1);
t.is(capturedString, join([
'CaptureFixture.call (capture-fixture.js:23:28)'
]));
});
test('captureString: with limit and startStack', t => {
const stack = new StackUtils({internals: internals(), cwd: fixtureDir});
const capture = new CaptureFixture(stack);
const capturedString = capture.redirect1('redirect2', 'call', 'captureString', 1, capture.call);
t.is(capturedString, join([
'CaptureFixture.redirect2 (capture-fixture.js:17:22)'
]));
});
test('capture returns an array of call sites', t => {
const stackUtil = new StackUtils({internals: internals(), cwd: fixtureDir});
const capture = new CaptureFixture(stackUtil);
const stack = capture.redirect1('call', 'capture').slice(0, 2);
t.is(stack[0].getFileName(), path.join(fixtureDir, 'capture-fixture.js'));
t.is(stack[0].getFunctionName(), 'CaptureFixture.call');
t.is(stack[1].getFunctionName(), 'CaptureFixture.redirect1');
});
test('capture: with limit', t => {
const stackUtil = new StackUtils({internals: internals(), cwd: fixtureDir});
const capture = new CaptureFixture(stackUtil);
const stack = capture.redirect1('redirect2', 'call', 'capture', 1);
t.is(stack.length, 1);
t.is(stack[0].getFunctionName(), 'CaptureFixture.call');
});
test('capture: with stackStart function', t => {
const stackUtil = new StackUtils({internals: internals(), cwd: fixtureDir});
const capture = new CaptureFixture(stackUtil);
const stack = capture.redirect1('redirect2', 'call', 'capture', capture.call);
t.true(stack.length > 1);
t.is(stack[0].getFunctionName(), 'CaptureFixture.redirect2');
});
test('capture: with limit and stackStart function', t => {
const stackUtil = new StackUtils({internals: internals(), cwd: fixtureDir});
const capture = new CaptureFixture(stackUtil);
const stack = capture.redirect1('redirect2', 'call', 'capture', 1, capture.call);
t.is(stack.length, 1);
t.is(stack[0].getFunctionName(), 'CaptureFixture.redirect2');
});
test('at', t => {
const stackUtil = new StackUtils({internals: internals(), cwd: fixtureDir});
const capture = new CaptureFixture(stackUtil);
const at = capture.redirect1('call', 'at');
t.same(at, {
file: 'capture-fixture.js',
line: 23,
column: 28,
type: 'CaptureFixture',
function: 'CaptureFixture.call',
method: 'call'
});
});
test('at: with stackStart', t => {
const stackUtil = new StackUtils({internals: internals(), cwd: __dirname});
const capture = new CaptureFixture(stackUtil);
const at = capture.redirect1('call', 'at', capture.call);
t.same(at, {
file: `fixtures/capture-fixture.js`,
line: 11,
column: 22,
type: 'CaptureFixture',
function: 'CaptureFixture.redirect1',
method: 'redirect1'
});
});
test('at: inside a constructor call', t => {
const stackUtil = new StackUtils({internals: internals(), cwd: fixtureDir});
const capture = new CaptureFixture(stackUtil);
const at = capture.const('call', 'at', capture.call);
// TODO: File an issue - if this assert fails, the power assert diagram renderer blows up.
t.same(at, {
file: 'capture-fixture.js',
line: 32,
column: 27,
constructor: true,
type: 'Constructor',
function: 'Constructor'
});
});
test('at: method on an [Object] instance', t => {
const stackUtil = new StackUtils({internals: internals(), cwd: fixtureDir});
const capture = new CaptureFixture(stackUtil);
const at = capture.const('obj', 'foo', 'call', 'at', capture.call);
t.same(at, {
file: 'capture-fixture.js',
line: 46,
column: 23,
function: 'obj.(anonymous function)',
method: 'foo'
});
});
test('at: returns empty object if #capture() returns an empty stack', t => {
const stackUtil = new StackUtils();
stackUtil.capture = function () {
return [];
};
t.same(stackUtil.at(), {});
});
test('at: eval', t => {
const stackUtil = new StackUtils({internals: internals(), cwd: fixtureDir});
const capture = new CaptureFixture(stackUtil);
const at = capture.eval('call', 'at', capture.call);
const expected = {
line: 1,
column: 14,
evalOrigin: 'eval at <anonymous> (' + path.join(fixtureDir, 'capture-fixture.js') + ':57:9)',
function: 'eval'
};
// TODO: There are some inconsistencies between this and how `parseLine` works.
if (version[0] < 4) {
expected.type = 'CaptureFixture';
expected.function = 'eval';
}
t.same(at, expected);
});
test('parseLine', t => {
const stack = new StackUtils({internals: internals(), cwd: '/user/dev/project'});
const capture = new CaptureFixture(stack);
t.same(stack.parseLine('foo'), null, 'should not match');
t.same(stack.parseLine(' at bar (/user/dev/project/foo.js:3:8)'), {
file: 'foo.js',
line: 3,
column: 8,
function: 'bar'
});
t.same(stack.parseLine(' at SomeClass.someFunc (/user/dev/project/foo.js:3:8)'), {
file: 'foo.js',
line: 3,
column: 8,
function: 'SomeClass.someFunc'
});
t.same(stack.parseLine(' at foo (/some/other/dir/file.js:3:8)'), {
file: '/some/other/dir/file.js',
line: 3,
column: 8,
function: 'foo'
});
// TODO: report issue - this also causes power-assert diagram renderer to fail
t.same(stack.parseLine(' at new Foo (/user/dev/project/foo.js:3:8)'), {
file: 'foo.js',
line: 3,
column: 8,
constructor: true,
function: 'Foo'
});
// EVAL
const evalStack = capture.eval('error', 'foo').stack.split('\n');
const expected = {
file: '<anonymous>',
line: 1,
column: 14,
evalOrigin: '<anonymous>',
evalLine: 57,
evalColumn: 9,
evalFile: path.join(fixtureDir, 'capture-fixture.js').replace(/\\/g, '/'),
function: 'eval'
};
if (version[0] < 4) {
expected.function = 'CaptureFixture.eval';
}
const actual = stack.parseLine(evalStack[2]);
t.same(actual, expected);
});
test('parseLine: handles native errors', t => {
t.same(StackUtils.parseLine(' at Error (native)'), {
native: true,
function: 'Error'
});
});
function linuxStack1() {
return [
'Error: foo',
' at foo (/user/dev/project/foo.js:3:8)',
' at bar (/user/dev/project/foo.js:7:2)',
' at bar (/user/dev/project/bar.js:4:2)',
' at Object.<anonymous> (/user/dev/project/bar.js:7:1)'
];
}
function windowsStack1() {
return [
'Error: foo',
' at foo (Z:\\user\\dev\\project\\foo.js:3:8)',
' at bar (Z:\\user\\dev\\project\\foo.js:7:2)',
' at bar (Z:\\user\\dev\\project\\bar.js:4:2)',
' at Object.<anonymous> (Z:\\user\\dev\\project\\bar.js:7:1)'
];
}
function internalStack() {
return [
' at Module._compile (module.js:398:26)',
' at Object.Module._extensions..js (module.js:405:10)',
' at Module.load (module.js:344:32)',
' at Function.Module._load (module.js:301:12)',
' at Function.Module.runMain (module.js:430:10)',
' at startup (node.js:141:18)'
];
}
function internals() {
return StackUtils.nodeInternals().concat([
/test\.js:[0-9]+:[0-9]+\)?$/,
/[\\\/]node_modules[\\\/]/
]);
}