Skip to content

Commit 44f1258

Browse files
committed
fix(local-variables): Improve test for local variables in out of app frames
Move the creation/cleanup of the out-of-app frame module file into the test setup/teardown.
1 parent 5847c60 commit 44f1258

File tree

3 files changed

+43
-36
lines changed

3 files changed

+43
-36
lines changed

dev-packages/node-integration-tests/suites/public-api/LocalVariables/local-variables-out-of-app-default.js

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,21 @@
33
const Sentry = require('@sentry/node');
44
const { loggingTransport } = require('@sentry-internal/node-integration-tests');
55

6-
// make sure to create the following file with the following content:
7-
// function out_of_app_function() {
8-
// const outOfAppVar = 'out of app value';
9-
// throw new Error('out-of-app error');
10-
// }
6+
const externalFunctionFile = require.resolve(
7+
'./node_modules/out-of-app-function.js'
8+
);
119

12-
// module.exports = { out_of_app_function };
13-
14-
const { out_of_app_function } = require('./node_modules/test-module/out-of-app-function.js');
10+
const { out_of_app_function } = require(externalFunctionFile);
1511

1612
function in_app_function() {
1713
const inAppVar = 'in app value';
18-
out_of_app_function();
14+
out_of_app_function(`${inAppVar} modified value`);
1915
}
2016

2117
Sentry.init({
2218
dsn: 'https://public@dsn.ingest.sentry.io/1337',
2319
transport: loggingTransport,
2420
includeLocalVariables: true,
25-
// either set each frame's in_app flag manually or import the `out_of_app_function` from a node_module directory
26-
// beforeSend: (event) => {
27-
// event.exception?.values?.[0]?.stacktrace?.frames?.forEach(frame => {
28-
// if (frame.function === 'out_of_app_function') {
29-
// frame.in_app = false;
30-
// }
31-
// });
32-
// return event;
33-
// },
3421
});
3522

3623
setTimeout(async () => {
@@ -39,7 +26,5 @@ setTimeout(async () => {
3926
} catch (e) {
4027
Sentry.captureException(e);
4128
await Sentry.flush();
42-
43-
return null;
4429
}
45-
}, 1000);
30+
}, 500);

dev-packages/node-integration-tests/suites/public-api/LocalVariables/local-variables-out-of-app.js

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
const Sentry = require('@sentry/node');
44
const { loggingTransport } = require('@sentry-internal/node-integration-tests');
55

6-
const { out_of_app_function } = require('./node_modules/test-module/out-of-app-function.js');
6+
const externalFunctionFile = require.resolve(
7+
'./node_modules/out-of-app-function.js'
8+
);
9+
10+
const { out_of_app_function } = require(externalFunctionFile);
711

812
Sentry.init({
913
dsn: 'https://public@dsn.ingest.sentry.io/1337',
@@ -14,20 +18,11 @@ Sentry.init({
1418
includeOutOfAppFrames: true,
1519
}),
1620
],
17-
// either set each frame's in_app flag manually or import the `out_of_app_function` from a node_module directory
18-
// beforeSend: (event) => {
19-
// event.exception?.values?.[0]?.stacktrace?.frames?.forEach(frame => {
20-
// if (frame.function === 'out_of_app_function') {
21-
// frame.in_app = false;
22-
// }
23-
// });
24-
// return event;
25-
// },
2621
});
2722

2823
function in_app_function() {
2924
const inAppVar = 'in app value';
30-
out_of_app_function();
25+
out_of_app_function(`${inAppVar} modified value`);
3126
}
3227

3328
setTimeout(async () => {
@@ -36,6 +31,5 @@ setTimeout(async () => {
3631
} catch (e) {
3732
Sentry.captureException(e);
3833
await Sentry.flush();
39-
return null;
4034
}
41-
}, 1000);
35+
}, 500);

dev-packages/node-integration-tests/suites/public-api/LocalVariables/test.ts

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
import { mkdirSync, rmdirSync, unlinkSync, writeFileSync } from 'fs';
12
import * as path from 'path';
2-
import { afterAll, describe, expect, test } from 'vitest';
3+
import { afterAll, beforeAll, describe, expect, test } from 'vitest';
34
import { conditionalTest } from '../../../utils';
45
import { cleanupChildProcesses, createRunner } from '../../../utils/runner';
56

@@ -39,8 +40,32 @@ const EXPECTED_LOCAL_VARIABLES_EVENT = {
3940
};
4041

4142
describe('LocalVariables integration', () => {
43+
const nodeModules = `${__dirname}/node_modules`;
44+
const externalModule = `${nodeModules}//out-of-app-function.js`;
45+
function cleanupExternalModuleFile() {
46+
try {
47+
unlinkSync(externalModule);
48+
// eslint-disable-next-line no-empty
49+
} catch {}
50+
try {
51+
rmdirSync(nodeModules);
52+
// eslint-disable-next-line no-empty
53+
} catch {}
54+
}
55+
56+
beforeAll(() => {
57+
cleanupExternalModuleFile();
58+
mkdirSync(nodeModules);
59+
writeFileSync(externalModule, `
60+
function out_of_app_function(passedArg) {
61+
const outOfAppVar = "out of app value " + passedArg.substring(13);
62+
throw new Error("out-of-app error");
63+
}
64+
module.exports = { out_of_app_function };`);
65+
});
4266
afterAll(() => {
4367
cleanupChildProcesses();
68+
cleanupExternalModuleFile();
4469
});
4570

4671
test('Should not include local variables by default', async () => {
@@ -140,7 +165,10 @@ describe('LocalVariables integration', () => {
140165
expect(inAppFrame?.vars).toEqual({ inAppVar: 'in app value' });
141166
expect(inAppFrame?.in_app).toEqual(true);
142167

143-
expect(outOfAppFrame?.vars).toEqual({ outOfAppVar: 'out of app value' });
168+
expect(outOfAppFrame?.vars).toEqual({
169+
outOfAppVar: 'out of app value modified value',
170+
passedArg: 'in app value modified value',
171+
});
144172
expect(outOfAppFrame?.in_app).toEqual(false);
145173
},
146174
})

0 commit comments

Comments
 (0)