Skip to content

Commit 02e1c1a

Browse files
author
Luca Forstner
authored
fix(utils): Normalize undefined to undefined instead of "[undefined]" (#8017)
1 parent 5c4963d commit 02e1c1a

File tree

4 files changed

+12
-17
lines changed

4 files changed

+12
-17
lines changed

packages/browser-integration-tests/suites/public-api/setContext/multiple_contexts/test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ sentryTest('should record multiple contexts', async ({ getLocalTestPath, page })
1616
baz: { qux: 'quux' },
1717
},
1818
context_2: { 1: 'foo', bar: false },
19-
context_4: '[undefined]',
2019
context_5: '[NaN]',
2120
context_6: 3.141592653589793,
2221
});

packages/replay/test/unit/coreHandlers/util/addBreadcrumbEvent.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ describe('Unit | coreHandlers | util | addBreadcrumbEvent', function () {
3939
category: 'console',
4040
message: 'Test message',
4141
thisIsNull: null,
42-
thisIsUndefined: '[undefined]',
42+
thisIsUndefined: undefined,
4343
circular: '[Circular ~]',
4444
timestamp: BASE_TIMESTAMP / 1000,
4545
},

packages/utils/src/normalize.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,10 @@ function visit(
7979
const [memoize, unmemoize] = memo;
8080

8181
// Get the simple cases out of the way first
82-
if (value === null || (['number', 'boolean', 'string'].includes(typeof value) && !isNaN(value))) {
82+
if (
83+
value == null || // this matches null and undefined -> eqeq not eqeqeq
84+
(['number', 'boolean', 'string'].includes(typeof value) && !isNaN(value))
85+
) {
8386
return value as Primitive;
8487
}
8588

@@ -220,11 +223,6 @@ function stringifyValue(
220223
return '[NaN]';
221224
}
222225

223-
// this catches `undefined` (but not `null`, which is a primitive and can be serialized on its own)
224-
if (value === void 0) {
225-
return '[undefined]';
226-
}
227-
228226
if (typeof value === 'function') {
229227
return `[Function: ${getFunctionName(value)}]`;
230228
}

packages/utils/test/normalize.test.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ describe('normalize()', () => {
1414
expect(normalize(42)).toEqual(42);
1515
expect(normalize(true)).toEqual(true);
1616
expect(normalize(null)).toEqual(null);
17+
expect(normalize(undefined)).toBeUndefined();
1718
});
1819

1920
test('return same object or arrays for referenced inputs', () => {
@@ -403,7 +404,6 @@ describe('normalize()', () => {
403404

404405
describe('changes unserializeable/global values/classes to their respective string representations', () => {
405406
test('primitive values', () => {
406-
expect(normalize(undefined)).toEqual('[undefined]');
407407
expect(normalize(NaN)).toEqual('[NaN]');
408408
expect(normalize(Symbol('dogs'))).toEqual('[Symbol(dogs)]');
409409
// `BigInt` doesn't exist in Node 8
@@ -425,28 +425,26 @@ describe('normalize()', () => {
425425
});
426426

427427
test('primitive values in objects/arrays', () => {
428-
expect(normalize(['foo', 42, undefined, NaN])).toEqual(['foo', 42, '[undefined]', '[NaN]']);
428+
expect(normalize(['foo', 42, NaN])).toEqual(['foo', 42, '[NaN]']);
429429
expect(
430430
normalize({
431431
foo: 42,
432-
bar: undefined,
433-
baz: NaN,
432+
bar: NaN,
434433
}),
435434
).toEqual({
436435
foo: 42,
437-
bar: '[undefined]',
438-
baz: '[NaN]',
436+
bar: '[NaN]',
439437
});
440438
});
441439

442440
test('primitive values in deep objects/arrays', () => {
443-
expect(normalize(['foo', 42, [[undefined]], [NaN]])).toEqual(['foo', 42, [['[undefined]']], ['[NaN]']]);
441+
expect(normalize(['foo', 42, [[undefined]], [NaN]])).toEqual(['foo', 42, [[undefined]], ['[NaN]']]);
444442
expect(
445443
normalize({
446444
foo: 42,
447445
bar: {
448446
baz: {
449-
quz: undefined,
447+
quz: null,
450448
},
451449
},
452450
wat: {
@@ -457,7 +455,7 @@ describe('normalize()', () => {
457455
foo: 42,
458456
bar: {
459457
baz: {
460-
quz: '[undefined]',
458+
quz: null,
461459
},
462460
},
463461
wat: {

0 commit comments

Comments
 (0)