Skip to content

Commit 5e0174b

Browse files
committed
Add some tests
1 parent 5d698a2 commit 5e0174b

File tree

4 files changed

+124
-1
lines changed

4 files changed

+124
-1
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/* eslint-disable no-unused-vars */
2+
const Sentry = require('@sentry/node');
3+
4+
Sentry.init({
5+
dsn: 'https://[email protected]/1337',
6+
includeStackLocals: true,
7+
beforeSend: event => {
8+
// eslint-disable-next-line no-console
9+
console.log(JSON.stringify(event));
10+
},
11+
});
12+
13+
process.on('uncaughtException', () => {
14+
// do nothing - this will prevent the Error below from closing this process
15+
});
16+
17+
class Some {
18+
two(name) {
19+
throw new Error('Enough!');
20+
}
21+
}
22+
23+
function one(name) {
24+
const arr = [1, '2', new Date()];
25+
const obj = {
26+
name,
27+
num: 5,
28+
};
29+
30+
const ty = new Some();
31+
32+
ty.two(name);
33+
}
34+
35+
one('some name');
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/* eslint-disable no-unused-vars */
2+
const Sentry = require('@sentry/node');
3+
4+
Sentry.init({
5+
dsn: 'https://[email protected]/1337',
6+
beforeSend: event => {
7+
// eslint-disable-next-line no-console
8+
console.log(JSON.stringify(event));
9+
},
10+
});
11+
12+
process.on('uncaughtException', () => {
13+
// do nothing - this will prevent the Error below from closing this process
14+
});
15+
16+
class Some {
17+
two(name) {
18+
throw new Error('Enough!');
19+
}
20+
}
21+
22+
function one(name) {
23+
const arr = [1, '2', new Date()];
24+
const obj = {
25+
name,
26+
num: 5,
27+
};
28+
29+
const ty = new Some();
30+
31+
ty.two(name);
32+
}
33+
34+
one('some name');
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { Event } from '@sentry/node';
2+
import * as childProcess from 'child_process';
3+
import * as path from 'path';
4+
5+
describe('LocalVariables integration', () => {
6+
test('Should not include local variables by default', done => {
7+
expect.assertions(2);
8+
9+
const testScriptPath = path.resolve(__dirname, 'no-local-variables.js');
10+
11+
childProcess.exec(`node ${testScriptPath}`, { encoding: 'utf8' }, (_, stdout) => {
12+
const event = JSON.parse(stdout) as Event;
13+
14+
const frames = event.exception?.values?.[0].stacktrace?.frames || [];
15+
const lastFrame = frames[frames.length - 1];
16+
17+
expect(lastFrame.vars).toBeUndefined();
18+
19+
const penultimateFrame = frames[frames.length - 2];
20+
21+
expect(penultimateFrame.vars).toBeUndefined();
22+
23+
done();
24+
});
25+
});
26+
27+
test('Should include local variables when enabled', done => {
28+
expect.assertions(4);
29+
30+
const testScriptPath = path.resolve(__dirname, 'local-variables.js');
31+
32+
childProcess.exec(`node ${testScriptPath}`, { encoding: 'utf8' }, (_, stdout) => {
33+
const event = JSON.parse(stdout) as Event;
34+
35+
const frames = event.exception?.values?.[0].stacktrace?.frames || [];
36+
const lastFrame = frames[frames.length - 1];
37+
38+
expect(lastFrame.function).toBe('Some.two');
39+
expect(lastFrame.vars).toEqual({ name: 'some name' });
40+
41+
const penultimateFrame = frames[frames.length - 2];
42+
43+
expect(penultimateFrame.function).toBe('one');
44+
expect(penultimateFrame.vars).toEqual({
45+
name: 'some name',
46+
arr: [1, '2', null],
47+
obj: { name: 'some name', num: 5 },
48+
ty: '<Some>',
49+
});
50+
51+
done();
52+
});
53+
});
54+
});

packages/node/src/integrations/localvariables.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ type PausedExceptionEvent = Debugger.PausedEventDataType & {
4545

4646
/** Could this be an anonymous function? */
4747
function isAnonymous(name: string | undefined): boolean {
48-
return !!name && ['', '?', '<anonymous>'].includes(name);
48+
return name !== undefined && ['', '?', '<anonymous>'].includes(name);
4949
}
5050

5151
/** Do the function names appear to match? */

0 commit comments

Comments
 (0)