Skip to content

Commit 3ad018d

Browse files
authored
chore(test): Disable Vitest globals (#15507)
We should not use Vitest globals except where they are required. This PR: - Disables `globals` in our root `vite.config.ts` - Fixes missing imports - Sets `globals: true` for React since there are load of test failures without it and I couldn't immedialy work out why
1 parent fda7103 commit 3ad018d

File tree

32 files changed

+66
-45
lines changed

32 files changed

+66
-45
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
import { cleanupChildProcesses, createRunner } from '../../../utils/runner';
2+
import { describe, afterAll, test } from 'vitest';
23

34
describe('ContextLines integration in CJS', () => {
45
afterAll(() => {
56
cleanupChildProcesses();
67
});
78

89
// Regression test for: https://github.com/getsentry/sentry-javascript/issues/14892
9-
test('does not leak open file handles', done => {
10-
createRunner(__dirname, 'scenario.ts')
10+
test('does not leak open file handles', async () => {
11+
await createRunner(__dirname, 'scenario.ts')
1112
.expectN(10, {
1213
event: {},
1314
})
14-
.start(done);
15+
.start()
16+
.completed();
1517
});
1618
});

dev-packages/node-integration-tests/suites/express-v5/multiple-routers/complex-router/test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { afterAll, test } from 'vitest';
1+
import { afterAll, test, describe } from 'vitest';
22
import { cleanupChildProcesses, createRunner } from '../../../../utils/runner';
33

44
afterAll(() => {

dev-packages/node-integration-tests/suites/tracing/apollo-graphql/useOperationNameForRootSpan/test.ts

+25-18
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { createRunner } from '../../../../utils/runner';
2+
import { describe, test, expect } from 'vitest'
23

34
// Graphql Instrumentation emits some spans by default on server start
45
const EXPECTED_START_SERVER_TRANSACTION = {
56
transaction: 'Test Server Start',
67
};
78

89
describe('GraphQL/Apollo Tests > useOperationNameForRootSpan', () => {
9-
test('useOperationNameForRootSpan works with single query operation', done => {
10+
test('useOperationNameForRootSpan works with single query operation', async () => {
1011
const EXPECTED_TRANSACTION = {
1112
transaction: 'GET /test-graphql (query GetHello)',
1213
spans: expect.arrayContaining([
@@ -24,13 +25,14 @@ describe('GraphQL/Apollo Tests > useOperationNameForRootSpan', () => {
2425
]),
2526
};
2627

27-
createRunner(__dirname, 'scenario-query.js')
28+
await createRunner(__dirname, 'scenario-query.js')
2829
.expect({ transaction: EXPECTED_START_SERVER_TRANSACTION })
2930
.expect({ transaction: EXPECTED_TRANSACTION })
30-
.start(done);
31+
.start()
32+
.completed();
3133
});
3234

33-
test('useOperationNameForRootSpan works with single mutation operation', done => {
35+
test('useOperationNameForRootSpan works with single mutation operation', async () => {
3436
const EXPECTED_TRANSACTION = {
3537
transaction: 'GET /test-graphql (mutation TestMutation)',
3638
spans: expect.arrayContaining([
@@ -50,13 +52,14 @@ describe('GraphQL/Apollo Tests > useOperationNameForRootSpan', () => {
5052
]),
5153
};
5254

53-
createRunner(__dirname, 'scenario-mutation.js')
55+
await createRunner(__dirname, 'scenario-mutation.js')
5456
.expect({ transaction: EXPECTED_START_SERVER_TRANSACTION })
5557
.expect({ transaction: EXPECTED_TRANSACTION })
56-
.start(done);
58+
.start()
59+
.completed();
5760
});
5861

59-
test('useOperationNameForRootSpan ignores an invalid root span', done => {
62+
test('useOperationNameForRootSpan ignores an invalid root span', async () => {
6063
const EXPECTED_TRANSACTION = {
6164
transaction: 'test span name',
6265
spans: expect.arrayContaining([
@@ -74,13 +77,14 @@ describe('GraphQL/Apollo Tests > useOperationNameForRootSpan', () => {
7477
]),
7578
};
7679

77-
createRunner(__dirname, 'scenario-invalid-root-span.js')
80+
await createRunner(__dirname, 'scenario-invalid-root-span.js')
7881
.expect({ transaction: EXPECTED_START_SERVER_TRANSACTION })
7982
.expect({ transaction: EXPECTED_TRANSACTION })
80-
.start(done);
83+
.start()
84+
.completed();
8185
});
8286

83-
test('useOperationNameForRootSpan works with single query operation without name', done => {
87+
test('useOperationNameForRootSpan works with single query operation without name', async () => {
8488
const EXPECTED_TRANSACTION = {
8589
transaction: 'GET /test-graphql (query)',
8690
spans: expect.arrayContaining([
@@ -97,13 +101,14 @@ describe('GraphQL/Apollo Tests > useOperationNameForRootSpan', () => {
97101
]),
98102
};
99103

100-
createRunner(__dirname, 'scenario-no-operation-name.js')
104+
await createRunner(__dirname, 'scenario-no-operation-name.js')
101105
.expect({ transaction: EXPECTED_START_SERVER_TRANSACTION })
102106
.expect({ transaction: EXPECTED_TRANSACTION })
103-
.start(done);
107+
.start()
108+
.completed();
104109
});
105110

106-
test('useOperationNameForRootSpan works with multiple query operations', done => {
111+
test('useOperationNameForRootSpan works with multiple query operations', async () => {
107112
const EXPECTED_TRANSACTION = {
108113
transaction: 'GET /test-graphql (query GetHello, query GetWorld)',
109114
spans: expect.arrayContaining([
@@ -132,21 +137,23 @@ describe('GraphQL/Apollo Tests > useOperationNameForRootSpan', () => {
132137
]),
133138
};
134139

135-
createRunner(__dirname, 'scenario-multiple-operations.js')
140+
await createRunner(__dirname, 'scenario-multiple-operations.js')
136141
.expect({ transaction: EXPECTED_START_SERVER_TRANSACTION })
137142
.expect({ transaction: EXPECTED_TRANSACTION })
138-
.start(done);
143+
.start()
144+
.completed();
139145
});
140146

141-
test('useOperationNameForRootSpan works with more than 5 query operations', done => {
147+
test('useOperationNameForRootSpan works with more than 5 query operations', async () => {
142148
const EXPECTED_TRANSACTION = {
143149
transaction:
144150
'GET /test-graphql (query GetHello1, query GetHello2, query GetHello3, query GetHello4, query GetHello5, +4)',
145151
};
146152

147-
createRunner(__dirname, 'scenario-multiple-operations-many.js')
153+
await createRunner(__dirname, 'scenario-multiple-operations-many.js')
148154
.expect({ transaction: EXPECTED_START_SERVER_TRANSACTION })
149155
.expect({ transaction: EXPECTED_TRANSACTION })
150-
.start(done);
156+
.start()
157+
.completed();
151158
});
152159
});

dev-packages/node-integration-tests/utils/assertions.ts

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type {
88
SessionAggregates,
99
TransactionEvent,
1010
} from '@sentry/core';
11+
import { expect } from 'vitest';
1112

1213
/**
1314
* Asserts against a Sentry Event ignoring non-deterministic properties

dev-packages/node-integration-tests/utils/index.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type * as http from 'http';
22
import { parseSemver } from '@sentry/core';
33
import type { EnvelopeItemType } from '@sentry/core';
4+
import { describe } from 'vitest';
45

56
const NODE_VERSION = parseSemver(process.versions.node).major;
67

@@ -31,17 +32,16 @@ export type DataCollectorOptions = {
3132
* Returns`describe` or `describe.skip` depending on allowed major versions of Node.
3233
*
3334
* @param {{ min?: number; max?: number }} allowedVersion
34-
* @return {*} {jest.Describe}
3535
*/
36-
export const conditionalTest = (allowedVersion: { min?: number; max?: number }): jest.Describe => {
36+
export function conditionalTest(allowedVersion: { min?: number; max?: number }): typeof describe | typeof describe.skip{
3737
if (!NODE_VERSION) {
3838
return describe.skip;
3939
}
4040

4141
return NODE_VERSION < (allowedVersion.min || -Infinity) || NODE_VERSION > (allowedVersion.max || Infinity)
4242
? describe.skip
4343
: describe;
44-
};
44+
}
4545

4646
/**
4747
* Parses response body containing an Envelope

packages/astro/test/server/index.server.test.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import { describe, expect, it } from 'vitest';
12
import sentryAstro from '../../src/index.server';
3+
24
describe('server SDK', () => {
35
it('exports the astro integration as a default export', () => {
46
const integration = sentryAstro();

packages/astro/test/server/sdk.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as SentryNode from '@sentry/node';
22
import { SDK_VERSION } from '@sentry/node';
3-
import { vi } from 'vitest';
3+
import { vi, describe, afterEach, expect, it } from 'vitest';
44

55
import { init } from '../../src/server/sdk';
66

packages/browser-utils/test/browser/browserMetrics.test.ts

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import {
99
spanToJSON,
1010
} from '@sentry/core';
1111
import type { Span } from '@sentry/core';
12+
import { describe, beforeEach, it, expect, beforeAll, afterAll } from 'vitest';
13+
1214
import { _addMeasureSpans, _addResourceSpans } from '../../src/metrics/browserMetrics';
1315
import { WINDOW } from '../../src/types';
1416
import { TestClient, getDefaultClientOptions } from '../utils/TestClient';

packages/browser-utils/test/browser/utils.test.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { SentrySpan, getCurrentScope, getIsolationScope, setCurrentClient, spanToJSON } from '@sentry/core';
2+
import { describe, beforeEach, it, expect, test } from 'vitest';
3+
24
import { extractNetworkProtocol, startAndEndSpan } from '../../src/metrics/utils';
35
import { TestClient, getDefaultClientOptions } from '../utils/TestClient';
46

packages/browser-utils/test/instrument/dom.test.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { describe, expect, it } from 'vitest';
2+
13
import { instrumentDOM } from '../../src/instrument/dom';
24
import { WINDOW } from '../../src/types';
35

packages/browser-utils/test/instrument/xhr.test.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { describe, expect, it } from 'vitest';
2+
13
import { instrumentXHR } from '../../src/instrument/xhr';
24
import { WINDOW } from '../../src/types';
35

packages/browser/test/sdk.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
/* eslint-disable @typescript-eslint/unbound-method */
66
import type { Mock } from 'vitest';
7-
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
7+
import { afterEach, beforeEach, describe, expect, it, vi, afterAll, test } from 'vitest';
88

99
import * as SentryCore from '@sentry/core';
1010
import { createTransport } from '@sentry/core';

packages/browser/test/tracing/request.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { MockInstance } from 'vitest';
2-
import { beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';
2+
import { beforeAll, beforeEach, describe, expect, it, vi, afterEach } from 'vitest';
33

44
import * as browserUtils from '@sentry-internal/browser-utils';
55
import * as utils from '@sentry/core';

packages/browser/test/transports/offline.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { describe, expect, it } from 'vitest';
1+
import { describe, expect, it, beforeAll } from 'vitest';
22

33
import 'fake-indexeddb/auto';
44

packages/browser/test/utils/featureFlags.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { FeatureFlag } from '@sentry/core';
22

33
import { getCurrentScope, logger } from '@sentry/core';
4-
import { vi } from 'vitest';
4+
import { vi, describe, it, afterEach, expect } from 'vitest';
55
import { insertFlagToScope, insertToFlagBuffer } from '../../src/utils/featureFlags';
66

77
describe('flags', () => {

packages/core/test/utils-hoist/envelope.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
spanToJSON,
88
} from '@sentry/core';
99
import { SentrySpan } from '@sentry/core';
10-
import { describe, expect, it, test, vi } from 'vitest';
10+
import { describe, expect, it, test, vi, afterEach } from 'vitest';
1111
import { getSentryCarrier } from '../../src/carrier';
1212
import {
1313
addItemToEnvelope,

packages/eslint-plugin-sdk/test/lib/rules/no-eq-empty.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { RuleTester } from 'eslint';
2-
import { describe } from 'vitest';
2+
import { describe, test } from 'vitest';
33

44
// @ts-expect-error untyped module
55
import rule from '../../../src/rules/no-eq-empty';

packages/feedback/test/core/getFeedback.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { vi, describe, it, expect } from 'vitest';
1+
import { vi, describe, it, expect, beforeEach } from 'vitest';
22

33
import { getCurrentScope } from '@sentry/core';
44
import { getFeedback } from '../../src/core/getFeedback';

packages/feedback/test/core/sendFeedback.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @vitest-environment jsdom
33
*/
4-
import { vi, describe, it, expect } from 'vitest';
4+
import { vi, describe, it, expect, beforeEach, afterAll } from 'vitest';
55

66
import {
77
addBreadcrumb,

packages/nestjs/test/integrations/nest.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { beforeEach, describe, expect, it, vi } from 'vitest';
1+
import { beforeEach, describe, expect, it, vi, afterEach } from 'vitest';
22

33
import * as core from '@sentry/core';
44
import { isPatched } from '../../src/integrations/helpers';

packages/react/vite.config.ts

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import baseConfig from '../../vite/vite.config';
55
export default defineConfig({
66
...baseConfig,
77
test: {
8+
globals: true,
89
...baseConfig.test,
910
},
1011
});

packages/replay-internal/test.setup.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { printDiffOrStringify } from 'jest-matcher-utils';
2-
import { vi } from 'vitest';
2+
import { vi, expect } from 'vitest';
33
import type { Mocked, MockedFunction } from 'vitest';
44

55
/* eslint-disable @typescript-eslint/no-unsafe-member-access */

packages/replay-internal/test/unit/util/logger.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { beforeEach, describe, expect, it } from 'vitest';
1+
import { beforeEach, describe, expect, it, vi } from 'vitest';
22

33
import * as SentryCore from '@sentry/core';
44
import { logger as coreLogger } from '@sentry/core';

packages/solidstart/test/client/errorboundary.test.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type * as SentryBrowser from '@sentry/browser';
33
import { createTransport, getCurrentScope, setCurrentClient } from '@sentry/core';
44
import { render } from '@solidjs/testing-library';
55
import userEvent from '@testing-library/user-event';
6-
import { vi } from 'vitest';
6+
import { vi, describe, beforeEach, afterEach, it, expect } from 'vitest';
77

88
import { ErrorBoundary } from 'solid-js';
99
import { BrowserClient, withSentryErrorBoundary } from '../../src/client';

packages/solidstart/test/client/sdk.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { SDK_VERSION } from '@sentry/solid';
22
import * as SentrySolid from '@sentry/solid';
33

4-
import { vi } from 'vitest';
4+
import { vi, describe, beforeEach, it, expect } from 'vitest';
55
import { init as solidStartInit } from '../../src/client';
66
import { solidRouterBrowserTracingIntegration } from '../../src/client/solidrouter';
77

packages/solidstart/test/client/solidrouter.test.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
import type { MemoryHistory } from '@solidjs/router';
1111
import { MemoryRouter, Navigate, Route, createMemoryHistory } from '@solidjs/router';
1212
import { render } from '@solidjs/testing-library';
13-
import { vi } from 'vitest';
13+
import { vi, describe, it, beforeEach, expect } from 'vitest';
1414

1515
import { BrowserClient } from '../../src/client';
1616
import { solidRouterBrowserTracingIntegration, withSentryRouterRouting } from '../../src/client/solidrouter';

packages/solidstart/test/server/errorboundary.test.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type * as SentryCore from '@sentry/core';
33
import { createTransport, getCurrentScope, setCurrentClient } from '@sentry/core';
44
import { render } from '@solidjs/testing-library';
55
import userEvent from '@testing-library/user-event';
6-
import { vi } from 'vitest';
6+
import { vi, describe, beforeEach, it, expect, afterEach } from 'vitest';
77

88
import { ErrorBoundary } from 'solid-js';
99
import { NodeClient, withSentryErrorBoundary } from '../../src/server';

packages/solidstart/test/server/middleware.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as SentryCore from '@sentry/core';
2-
import { beforeEach, describe, it, vi } from 'vitest';
2+
import { beforeEach, describe, it, vi, expect } from 'vitest';
33
import { sentryBeforeResponseMiddleware } from '../../src/server';
44
import type { ResponseMiddlewareResponse } from '../../src/server';
55

packages/solidstart/test/server/solidrouter.test.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { MemoryHistory } from '@solidjs/router';
22
import { MemoryRouter, Route, createMemoryHistory } from '@solidjs/router';
33
import { render } from '@solidjs/testing-library';
4-
import { vi } from 'vitest';
4+
import { vi, describe, expect, it } from 'vitest';
55

66
import { withSentryRouterRouting as withSentryClientRouterRouting } from '../../src/client/solidrouter';
77
import { withSentryRouterRouting as withSentryServerRouterRouting } from '../../src/server/solidrouter';

packages/solidstart/test/vite/buildInstrumentation.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { UserConfig } from 'vite';
2-
import { describe, expect, it, vi } from 'vitest';
2+
import { describe, expect, it, vi, beforeEach } from 'vitest';
33
import { makeBuildInstrumentationFilePlugin } from '../../src/vite/buildInstrumentationFile';
44

55
const fsAccessMock = vi.fn();

packages/sveltekit/test/server/rewriteFramesIntegration.test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { rewriteFramesIntegration } from '@sentry/browser';
22
import { basename } from '@sentry/core';
33
import type { Event, StackFrame } from '@sentry/core';
4+
import { describe, expect, it } from 'vitest';
45

56
import { rewriteFramesIteratee } from '../../src/server-common/rewriteFramesIntegration';
67
import type { GlobalWithSentryValues } from '../../src/vite/injectGlobalValues';

vite/vite.config.ts

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ export default defineConfig({
55
__DEBUG_BUILD__: true,
66
},
77
test: {
8-
globals: true,
98
coverage: {
109
enabled: true,
1110
reportsDirectory: './coverage',

0 commit comments

Comments
 (0)