Skip to content

Commit 1bb86db

Browse files
authored
test(ref): Avoid some unnecessary conditions in tests (#12493)
This updates some tests to avoid unnecessary conditions. Extracted this out while looking at enabling eslint no-uncessary-condition, as this can be done without much reviewing.
1 parent 1a2a33c commit 1bb86db

File tree

20 files changed

+55
-55
lines changed

20 files changed

+55
-55
lines changed

packages/browser/test/unit/eventbuilder.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ describe('eventFromUnknownInput', () => {
4444

4545
const event = eventFromUnknownInput(defaultStackParser, deepObject);
4646

47-
expect(event?.extra?.__serialized__).toEqual({
47+
expect(event.extra?.__serialized__).toEqual({
4848
a: {
4949
b: {
5050
c: {

packages/browser/test/unit/index.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -357,10 +357,10 @@ describe('SentryBrowser initialization', () => {
357357

358358
const sdkData = getClient()?.getOptions()._metadata?.sdk || {};
359359

360-
expect(sdkData?.name).toBe('sentry.javascript.browser');
361-
expect(sdkData?.packages?.[0]?.name).toBe('npm:@sentry/browser');
362-
expect(sdkData?.packages?.[0]?.version).toBe(SDK_VERSION);
363-
expect(sdkData?.version).toBe(SDK_VERSION);
360+
expect(sdkData.name).toBe('sentry.javascript.browser');
361+
expect(sdkData.packages?.[0]?.name).toBe('npm:@sentry/browser');
362+
expect(sdkData.packages?.[0]?.version).toBe(SDK_VERSION);
363+
expect(sdkData.version).toBe(SDK_VERSION);
364364
});
365365

366366
it('uses SDK source from window for package name', () => {

packages/browser/test/unit/profiling/integration.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ describe('BrowserProfilingIntegration', () => {
5656

5757
expect(send).toHaveBeenCalledTimes(1);
5858

59-
const profile = send.mock.calls?.[0]?.[0]?.[1]?.[1]?.[1];
60-
const transaction = send.mock.calls?.[0]?.[0]?.[1]?.[0]?.[1];
59+
const profile = send.mock.calls[0]?.[0]?.[1]?.[1]?.[1];
60+
const transaction = send.mock.calls[0]?.[0]?.[1]?.[0]?.[1];
6161
const profile_timestamp_ms = new Date(profile.timestamp).getTime();
6262
const transaction_timestamp_ms = new Date(transaction.start_timestamp * 1e3).getTime();
6363

packages/browser/test/unit/tracing/browserTracingIntegration.test.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -672,8 +672,8 @@ describe('browserTracingIntegration', () => {
672672
traceId: expect.stringMatching(/[a-f0-9]{32}/),
673673
});
674674

675-
expect(newIsolationScopePropCtx?.traceId).not.toEqual(oldIsolationScopePropCtx?.traceId);
676-
expect(newCurrentScopePropCtx?.traceId).not.toEqual(oldCurrentScopePropCtx?.traceId);
675+
expect(newIsolationScopePropCtx.traceId).not.toEqual(oldIsolationScopePropCtx.traceId);
676+
expect(newCurrentScopePropCtx.traceId).not.toEqual(oldCurrentScopePropCtx.traceId);
677677
});
678678

679679
it("saves the span's positive sampling decision and its DSC on the propagationContext when the span finishes", () => {
@@ -701,16 +701,16 @@ describe('browserTracingIntegration', () => {
701701

702702
const propCtxAfterEnd = getCurrentScope().getPropagationContext();
703703
expect(propCtxAfterEnd).toStrictEqual({
704-
spanId: propCtxBeforeEnd?.spanId,
705-
traceId: propCtxBeforeEnd?.traceId,
704+
spanId: propCtxBeforeEnd.spanId,
705+
traceId: propCtxBeforeEnd.traceId,
706706
sampled: true,
707707
dsc: {
708708
environment: 'production',
709709
public_key: 'examplePublicKey',
710710
sample_rate: '1',
711711
sampled: 'true',
712712
transaction: 'mySpan',
713-
trace_id: propCtxBeforeEnd?.traceId,
713+
trace_id: propCtxBeforeEnd.traceId,
714714
},
715715
});
716716
});
@@ -740,16 +740,16 @@ describe('browserTracingIntegration', () => {
740740

741741
const propCtxAfterEnd = getCurrentScope().getPropagationContext();
742742
expect(propCtxAfterEnd).toStrictEqual({
743-
spanId: propCtxBeforeEnd?.spanId,
744-
traceId: propCtxBeforeEnd?.traceId,
743+
spanId: propCtxBeforeEnd.spanId,
744+
traceId: propCtxBeforeEnd.traceId,
745745
sampled: false,
746746
dsc: {
747747
environment: 'production',
748748
public_key: 'examplePublicKey',
749749
sample_rate: '0',
750750
sampled: 'false',
751751
transaction: 'mySpan',
752-
trace_id: propCtxBeforeEnd?.traceId,
752+
trace_id: propCtxBeforeEnd.traceId,
753753
},
754754
});
755755
});
@@ -950,7 +950,7 @@ describe('browserTracingIntegration', () => {
950950
client.emit('idleSpanEnableAutoFinish', idleSpan!);
951951

952952
const span = startInactiveSpan({ name: 'inner1' });
953-
span?.end(); // activities = 0
953+
span.end(); // activities = 0
954954

955955
// inner1 is now ended, all good
956956
expect(spans).toHaveLength(1);
@@ -985,7 +985,7 @@ describe('browserTracingIntegration', () => {
985985
client.emit('idleSpanEnableAutoFinish', idleSpan!);
986986

987987
const span = startInactiveSpan({ name: 'inner1' });
988-
span?.end(); // activities = 0
988+
span.end(); // activities = 0
989989

990990
// inner1 is now ended, all good
991991
expect(spans).toHaveLength(1);

packages/core/test/lib/integrations/functiontostring.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ describe('FunctionToString', () => {
2929
expect(foo.bar.toString()).not.toBe(originalFunction);
3030

3131
const fts = functionToStringIntegration();
32-
getClient()?.addIntegration?.(fts);
32+
getClient()?.addIntegration(fts);
3333

3434
expect(foo.bar.toString()).toBe(originalFunction);
3535
});

packages/core/test/lib/tracing/idleSpan.test.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,13 @@ describe('startIdleSpan', () => {
7272
startSpanManual({ name: 'inner1' }, span => {
7373
const childSpan = startInactiveSpan({ name: 'inner2' });
7474

75-
span?.end();
75+
span.end();
7676
jest.advanceTimersByTime(TRACING_DEFAULTS.idleTimeout + 1);
7777

7878
// Idle span is still recording
7979
expect(idleSpan.isRecording()).toBe(true);
8080

81-
childSpan?.end();
81+
childSpan.end();
8282
jest.advanceTimersByTime(TRACING_DEFAULTS.idleTimeout + 1);
8383

8484
// Now it is finished!
@@ -348,7 +348,7 @@ describe('startIdleSpan', () => {
348348
const idleSpan = startIdleSpan({ name: 'idle span' });
349349
expect(idleSpan).toBeDefined();
350350

351-
idleSpan?.end();
351+
idleSpan.end();
352352

353353
expect(recordDroppedEventSpy).toHaveBeenCalledWith('sample_rate', 'transaction');
354354
});
@@ -649,13 +649,13 @@ describe('startIdleSpan', () => {
649649
expect(idleSpan).toBeDefined();
650650

651651
const span1 = startInactiveSpan({ name: 'span1', startTime: 1001 });
652-
span1?.end(1005);
652+
span1.end(1005);
653653

654654
const span2 = startInactiveSpan({ name: 'span2', startTime: 1002 });
655-
span2?.end(1100);
655+
span2.end(1100);
656656

657657
const span3 = startInactiveSpan({ name: 'span1', startTime: 1050 });
658-
span3?.end(1060);
658+
span3.end(1060);
659659

660660
expect(getActiveSpan()).toBe(idleSpan);
661661

@@ -669,13 +669,13 @@ describe('startIdleSpan', () => {
669669
expect(idleSpan).toBeDefined();
670670

671671
const span1 = startInactiveSpan({ name: 'span1', startTime: 1001 });
672-
span1?.end(1005);
672+
span1.end(1005);
673673

674674
const span2 = startInactiveSpan({ name: 'span2', startTime: 1002 });
675-
span2?.end(1100);
675+
span2.end(1100);
676676

677677
const span3 = startInactiveSpan({ name: 'span1', startTime: 1050 });
678-
span3?.end(1060);
678+
span3.end(1060);
679679

680680
expect(getActiveSpan()).toBe(idleSpan);
681681

@@ -689,13 +689,13 @@ describe('startIdleSpan', () => {
689689
expect(idleSpan).toBeDefined();
690690

691691
const span1 = startInactiveSpan({ name: 'span1', startTime: 999_999_999 });
692-
span1?.end(1005);
692+
span1.end(1005);
693693

694694
const span2 = startInactiveSpan({ name: 'span2', startTime: 1002 });
695-
span2?.end(1100);
695+
span2.end(1100);
696696

697697
const span3 = startInactiveSpan({ name: 'span1', startTime: 1050 });
698-
span3?.end(1060);
698+
span3.end(1060);
699699

700700
expect(getActiveSpan()).toBe(idleSpan);
701701

packages/core/test/lib/tracing/trace.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -995,7 +995,7 @@ describe('startInactiveSpan', () => {
995995
startSpan({ name: 'outer transaction' }, () => {
996996
startSpan({ name: 'inner span' }, () => {
997997
const innerTransaction = startInactiveSpan({ name: 'inner transaction', forceTransaction: true });
998-
innerTransaction?.end();
998+
innerTransaction.end();
999999
});
10001000
});
10011001

@@ -1516,10 +1516,10 @@ describe('span hooks', () => {
15161516

15171517
startSpanManual({ name: 'span5' }, span => {
15181518
startInactiveSpan({ name: 'span4' });
1519-
span?.end();
1519+
span.end();
15201520
});
15211521

1522-
span?.end();
1522+
span.end();
15231523
});
15241524
});
15251525

packages/core/test/mocks/client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export class TestClient extends BaseClient<TestClientOptions> {
6363
};
6464

6565
const frames = this._options.stackParser(exception.stack || '', 1);
66-
if (frames.length && event?.exception?.values?.[0]) {
66+
if (frames.length && event.exception?.values?.[0]) {
6767
event.exception.values[0] = { ...event.exception.values[0], stacktrace: { frames } };
6868
}
6969

packages/core/test/mocks/integration.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export class TestIntegration implements Integration {
99

1010
public setupOnce(): void {
1111
const eventProcessor: EventProcessor = (event: Event) => {
12-
if (!getClient()?.getIntegrationByName?.('TestIntegration')) {
12+
if (!getClient()?.getIntegrationByName('TestIntegration')) {
1313
return event;
1414
}
1515

packages/node/test/integration/scope.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ describe('Integration | Scope', () => {
4242
scope2.setTag('tag3', 'val3');
4343

4444
Sentry.startSpan({ name: 'outer' }, span => {
45-
expect(getCapturedScopesOnSpan(span)?.scope).toBe(enableTracing ? scope2 : undefined);
45+
expect(getCapturedScopesOnSpan(span).scope).toBe(enableTracing ? scope2 : undefined);
4646

4747
spanId = span.spanContext().spanId;
4848
traceId = span.spanContext().traceId;

packages/opentelemetry/test/integration/scope.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ describe('Integration | Scope', () => {
4949
scope2.setTag('tag3', 'val3');
5050

5151
startSpan({ name: 'outer' }, span => {
52-
expect(getCapturedScopesOnSpan(span)?.scope).toBe(enableTracing ? scope2 : undefined);
52+
expect(getCapturedScopesOnSpan(span).scope).toBe(enableTracing ? scope2 : undefined);
5353

5454
spanId = span.spanContext().spanId;
5555
traceId = span.spanContext().traceId;

packages/opentelemetry/test/trace.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,7 @@ describe('trace', () => {
566566
startSpan({ name: 'outer transaction' }, () => {
567567
startSpan({ name: 'inner span' }, () => {
568568
const innerTransaction = startInactiveSpan({ name: 'inner transaction', forceTransaction: true });
569-
innerTransaction?.end();
569+
innerTransaction.end();
570570
});
571571
});
572572

packages/react/test/reactrouterv3.test.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ describe('browserTracingReactRouterV3', () => {
123123
client.init();
124124
render(<Router history={history}>{routes}</Router>);
125125

126-
expect(getCurrentScope().getScopeData()?.transactionName).toEqual('/');
126+
expect(getCurrentScope().getScopeData().transactionName).toEqual('/');
127127
});
128128

129129
it('starts a navigation transaction', () => {
@@ -213,13 +213,13 @@ describe('browserTracingReactRouterV3', () => {
213213
client.init();
214214
const { container } = render(<Router history={history}>{routes}</Router>);
215215

216-
expect(getCurrentScope().getScopeData()?.transactionName).toEqual('/');
216+
expect(getCurrentScope().getScopeData().transactionName).toEqual('/');
217217

218218
act(() => {
219219
history.push('/users/123');
220220
});
221221
expect(container.innerHTML).toContain('123');
222222

223-
expect(getCurrentScope().getScopeData()?.transactionName).toEqual('/users/:userid');
223+
expect(getCurrentScope().getScopeData().transactionName).toEqual('/users/:userid');
224224
});
225225
});

packages/react/test/reactrouterv4.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ describe('browserTracingReactRouterV4', () => {
9595

9696
client.init();
9797

98-
expect(getCurrentScope().getScopeData()?.transactionName).toEqual('/');
98+
expect(getCurrentScope().getScopeData().transactionName).toEqual('/');
9999
});
100100

101101
it('starts a navigation transaction', () => {

packages/react/test/reactrouterv5.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ describe('browserTracingReactRouterV5', () => {
9595

9696
client.init();
9797

98-
expect(getCurrentScope().getScopeData()?.transactionName).toEqual('/');
98+
expect(getCurrentScope().getScopeData().transactionName).toEqual('/');
9999
});
100100

101101
it('starts a navigation transaction', () => {

packages/react/test/reactrouterv6.4.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ describe('reactRouterV6BrowserTracingIntegration (v6.4)', () => {
152152
// @ts-expect-error router is fine
153153
render(<RouterProvider router={router} />);
154154

155-
expect(getCurrentScope().getScopeData()?.transactionName).toEqual('/');
155+
expect(getCurrentScope().getScopeData().transactionName).toEqual('/');
156156
});
157157

158158
it('starts a navigation transaction', () => {
@@ -658,7 +658,7 @@ describe('reactRouterV6BrowserTracingIntegration (v6.4)', () => {
658658
// @ts-expect-error router is fine
659659
render(<RouterProvider router={router} />);
660660

661-
expect(getCurrentScope().getScopeData()?.transactionName).toEqual('/about');
661+
expect(getCurrentScope().getScopeData().transactionName).toEqual('/about');
662662
});
663663
});
664664
});

packages/react/test/reactrouterv6.test.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ describe('reactRouterV6BrowserTracingIntegration', () => {
137137
</MemoryRouter>,
138138
);
139139

140-
expect(getCurrentScope().getScopeData()?.transactionName).toEqual('/');
140+
expect(getCurrentScope().getScopeData().transactionName).toEqual('/');
141141
});
142142

143143
it('skips pageload transaction with `instrumentPageLoad: false`', () => {
@@ -417,7 +417,7 @@ describe('reactRouterV6BrowserTracingIntegration', () => {
417417
</MemoryRouter>,
418418
);
419419

420-
expect(getCurrentScope().getScopeData()?.transactionName).toBe('/about/:page');
420+
expect(getCurrentScope().getScopeData().transactionName).toBe('/about/:page');
421421
});
422422
});
423423

@@ -493,7 +493,7 @@ describe('reactRouterV6BrowserTracingIntegration', () => {
493493
</MemoryRouter>,
494494
);
495495

496-
expect(getCurrentScope().getScopeData()?.transactionName).toEqual('/');
496+
expect(getCurrentScope().getScopeData().transactionName).toEqual('/');
497497
});
498498

499499
it('skips pageload transaction with `instrumentPageLoad: false`', () => {
@@ -997,7 +997,7 @@ describe('reactRouterV6BrowserTracingIntegration', () => {
997997
</MemoryRouter>,
998998
);
999999

1000-
expect(getCurrentScope().getScopeData()?.transactionName).toBe('/about');
1000+
expect(getCurrentScope().getScopeData().transactionName).toBe('/about');
10011001
});
10021002
});
10031003
});

packages/replay-internal/test/integration/session.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ describe('Integration | session', () => {
120120
const initialSession = { ...replay.session } as Session;
121121

122122
expect(mockRecord).toHaveBeenCalledTimes(1);
123-
expect(initialSession?.id).toBeDefined();
123+
expect(initialSession.id).toBeDefined();
124124
expect(replay.getContext()).toEqual(
125125
expect.objectContaining({
126126
initialUrl: 'http://localhost:3000/',
@@ -230,7 +230,7 @@ describe('Integration | session', () => {
230230
it('pauses and resumes a session if user has been idle for more than SESSION_IDLE_PASUE_DURATION and comes back to click their mouse', async () => {
231231
const initialSession = { ...replay.session } as Session;
232232

233-
expect(initialSession?.id).toBeDefined();
233+
expect(initialSession.id).toBeDefined();
234234
expect(replay.getContext()).toEqual(
235235
expect.objectContaining({
236236
initialUrl: 'http://localhost:3000/',
@@ -327,7 +327,7 @@ describe('Integration | session', () => {
327327

328328
const initialSession = { ...replay.session } as Session;
329329

330-
expect(initialSession?.id).toBeDefined();
330+
expect(initialSession.id).toBeDefined();
331331
expect(replay.getContext()).toMatchObject({
332332
initialUrl: 'http://localhost:3000/',
333333
initialTimestamp: BASE_TIMESTAMP,

packages/replay-internal/test/unit/coreHandlers/handleNetworkBreadcrumbs.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ function getMockResponse(contentLength?: string, body?: string, headers?: Record
3737
const response = {
3838
headers: {
3939
has: (prop: string) => {
40-
return !!internalHeaders[prop?.toLowerCase() ?? ''];
40+
return !!internalHeaders[prop.toLowerCase() ?? ''];
4141
},
4242
get: (prop: string) => {
43-
return internalHeaders[prop?.toLowerCase() ?? ''];
43+
return internalHeaders[prop.toLowerCase() ?? ''];
4444
},
4545
},
4646
clone: () => response,

0 commit comments

Comments
 (0)