Skip to content

fix(utils): Normalize undefined to undefined instead of "[undefined]" #8017

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ sentryTest('should record multiple contexts', async ({ getLocalTestPath, page })
baz: { qux: 'quux' },
},
context_2: { 1: 'foo', bar: false },
context_4: '[undefined]',
context_5: '[NaN]',
context_6: 3.141592653589793,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe('Unit | coreHandlers | util | addBreadcrumbEvent', function () {
category: 'console',
message: 'Test message',
thisIsNull: null,
thisIsUndefined: '[undefined]',
thisIsUndefined: undefined,
circular: '[Circular ~]',
timestamp: BASE_TIMESTAMP / 1000,
},
Expand Down
10 changes: 4 additions & 6 deletions packages/utils/src/normalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,10 @@ function visit(
const [memoize, unmemoize] = memo;

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

Expand Down Expand Up @@ -220,11 +223,6 @@ function stringifyValue(
return '[NaN]';
}

// this catches `undefined` (but not `null`, which is a primitive and can be serialized on its own)
if (value === void 0) {
return '[undefined]';
}

if (typeof value === 'function') {
return `[Function: ${getFunctionName(value)}]`;
}
Expand Down
16 changes: 7 additions & 9 deletions packages/utils/test/normalize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ describe('normalize()', () => {
expect(normalize(42)).toEqual(42);
expect(normalize(true)).toEqual(true);
expect(normalize(null)).toEqual(null);
expect(normalize(undefined)).toBeUndefined();
});

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

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

test('primitive values in objects/arrays', () => {
expect(normalize(['foo', 42, undefined, NaN])).toEqual(['foo', 42, '[undefined]', '[NaN]']);
expect(normalize(['foo', 42, NaN])).toEqual(['foo', 42, '[NaN]']);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm just thinking why not keep the undefined here?

Suggested change
expect(normalize(['foo', 42, NaN])).toEqual(['foo', 42, '[NaN]']);
expect(normalize(['foo', 42, undefined, NaN])).toEqual(['foo', 42, undefined, '[NaN]']);

I would expect this to pass, or not?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

youre right!

expect(
normalize({
foo: 42,
bar: undefined,
baz: NaN,
bar: NaN,
}),
).toEqual({
foo: 42,
bar: '[undefined]',
baz: '[NaN]',
bar: '[NaN]',
});
});

test('primitive values in deep objects/arrays', () => {
expect(normalize(['foo', 42, [[undefined]], [NaN]])).toEqual(['foo', 42, [['[undefined]']], ['[NaN]']]);
expect(normalize(['foo', 42, [[undefined]], [NaN]])).toEqual(['foo', 42, [[undefined]], ['[NaN]']]);
expect(
normalize({
foo: 42,
bar: {
baz: {
quz: undefined,
quz: null,
},
},
wat: {
Expand All @@ -457,7 +455,7 @@ describe('normalize()', () => {
foo: 42,
bar: {
baz: {
quz: '[undefined]',
quz: null,
},
},
wat: {
Expand Down