Skip to content

Commit

Permalink
chore(test): Disable Vitest globals (#15507)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
timfish authored Feb 26, 2025
1 parent fda7103 commit 3ad018d
Show file tree
Hide file tree
Showing 32 changed files with 66 additions and 45 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import { cleanupChildProcesses, createRunner } from '../../../utils/runner';
import { describe, afterAll, test } from 'vitest';

describe('ContextLines integration in CJS', () => {
afterAll(() => {
cleanupChildProcesses();
});

// Regression test for: https://github.com/getsentry/sentry-javascript/issues/14892
test('does not leak open file handles', done => {
createRunner(__dirname, 'scenario.ts')
test('does not leak open file handles', async () => {
await createRunner(__dirname, 'scenario.ts')
.expectN(10, {
event: {},
})
.start(done);
.start()
.completed();
});
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { afterAll, test } from 'vitest';
import { afterAll, test, describe } from 'vitest';
import { cleanupChildProcesses, createRunner } from '../../../../utils/runner';

afterAll(() => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { createRunner } from '../../../../utils/runner';
import { describe, test, expect } from 'vitest'

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

describe('GraphQL/Apollo Tests > useOperationNameForRootSpan', () => {
test('useOperationNameForRootSpan works with single query operation', done => {
test('useOperationNameForRootSpan works with single query operation', async () => {
const EXPECTED_TRANSACTION = {
transaction: 'GET /test-graphql (query GetHello)',
spans: expect.arrayContaining([
Expand All @@ -24,13 +25,14 @@ describe('GraphQL/Apollo Tests > useOperationNameForRootSpan', () => {
]),
};

createRunner(__dirname, 'scenario-query.js')
await createRunner(__dirname, 'scenario-query.js')
.expect({ transaction: EXPECTED_START_SERVER_TRANSACTION })
.expect({ transaction: EXPECTED_TRANSACTION })
.start(done);
.start()
.completed();
});

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

createRunner(__dirname, 'scenario-mutation.js')
await createRunner(__dirname, 'scenario-mutation.js')
.expect({ transaction: EXPECTED_START_SERVER_TRANSACTION })
.expect({ transaction: EXPECTED_TRANSACTION })
.start(done);
.start()
.completed();
});

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

createRunner(__dirname, 'scenario-invalid-root-span.js')
await createRunner(__dirname, 'scenario-invalid-root-span.js')
.expect({ transaction: EXPECTED_START_SERVER_TRANSACTION })
.expect({ transaction: EXPECTED_TRANSACTION })
.start(done);
.start()
.completed();
});

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

createRunner(__dirname, 'scenario-no-operation-name.js')
await createRunner(__dirname, 'scenario-no-operation-name.js')
.expect({ transaction: EXPECTED_START_SERVER_TRANSACTION })
.expect({ transaction: EXPECTED_TRANSACTION })
.start(done);
.start()
.completed();
});

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

createRunner(__dirname, 'scenario-multiple-operations.js')
await createRunner(__dirname, 'scenario-multiple-operations.js')
.expect({ transaction: EXPECTED_START_SERVER_TRANSACTION })
.expect({ transaction: EXPECTED_TRANSACTION })
.start(done);
.start()
.completed();
});

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

createRunner(__dirname, 'scenario-multiple-operations-many.js')
await createRunner(__dirname, 'scenario-multiple-operations-many.js')
.expect({ transaction: EXPECTED_START_SERVER_TRANSACTION })
.expect({ transaction: EXPECTED_TRANSACTION })
.start(done);
.start()
.completed();
});
});
1 change: 1 addition & 0 deletions dev-packages/node-integration-tests/utils/assertions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type {
SessionAggregates,
TransactionEvent,
} from '@sentry/core';
import { expect } from 'vitest';

/**
* Asserts against a Sentry Event ignoring non-deterministic properties
Expand Down
6 changes: 3 additions & 3 deletions dev-packages/node-integration-tests/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type * as http from 'http';
import { parseSemver } from '@sentry/core';
import type { EnvelopeItemType } from '@sentry/core';
import { describe } from 'vitest';

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

Expand Down Expand Up @@ -31,17 +32,16 @@ export type DataCollectorOptions = {
* Returns`describe` or `describe.skip` depending on allowed major versions of Node.
*
* @param {{ min?: number; max?: number }} allowedVersion
* @return {*} {jest.Describe}
*/
export const conditionalTest = (allowedVersion: { min?: number; max?: number }): jest.Describe => {
export function conditionalTest(allowedVersion: { min?: number; max?: number }): typeof describe | typeof describe.skip{
if (!NODE_VERSION) {
return describe.skip;
}

return NODE_VERSION < (allowedVersion.min || -Infinity) || NODE_VERSION > (allowedVersion.max || Infinity)
? describe.skip
: describe;
};
}

/**
* Parses response body containing an Envelope
Expand Down
2 changes: 2 additions & 0 deletions packages/astro/test/server/index.server.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { describe, expect, it } from 'vitest';
import sentryAstro from '../../src/index.server';

describe('server SDK', () => {
it('exports the astro integration as a default export', () => {
const integration = sentryAstro();
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/test/server/sdk.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as SentryNode from '@sentry/node';
import { SDK_VERSION } from '@sentry/node';
import { vi } from 'vitest';
import { vi, describe, afterEach, expect, it } from 'vitest';

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

Expand Down
2 changes: 2 additions & 0 deletions packages/browser-utils/test/browser/browserMetrics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {
spanToJSON,
} from '@sentry/core';
import type { Span } from '@sentry/core';
import { describe, beforeEach, it, expect, beforeAll, afterAll } from 'vitest';

import { _addMeasureSpans, _addResourceSpans } from '../../src/metrics/browserMetrics';
import { WINDOW } from '../../src/types';
import { TestClient, getDefaultClientOptions } from '../utils/TestClient';
Expand Down
2 changes: 2 additions & 0 deletions packages/browser-utils/test/browser/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { SentrySpan, getCurrentScope, getIsolationScope, setCurrentClient, spanToJSON } from '@sentry/core';
import { describe, beforeEach, it, expect, test } from 'vitest';

import { extractNetworkProtocol, startAndEndSpan } from '../../src/metrics/utils';
import { TestClient, getDefaultClientOptions } from '../utils/TestClient';

Expand Down
2 changes: 2 additions & 0 deletions packages/browser-utils/test/instrument/dom.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { describe, expect, it } from 'vitest';

import { instrumentDOM } from '../../src/instrument/dom';
import { WINDOW } from '../../src/types';

Expand Down
2 changes: 2 additions & 0 deletions packages/browser-utils/test/instrument/xhr.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { describe, expect, it } from 'vitest';

import { instrumentXHR } from '../../src/instrument/xhr';
import { WINDOW } from '../../src/types';

Expand Down
2 changes: 1 addition & 1 deletion packages/browser/test/sdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

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

import * as SentryCore from '@sentry/core';
import { createTransport } from '@sentry/core';
Expand Down
2 changes: 1 addition & 1 deletion packages/browser/test/tracing/request.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { MockInstance } from 'vitest';
import { beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';
import { beforeAll, beforeEach, describe, expect, it, vi, afterEach } from 'vitest';

import * as browserUtils from '@sentry-internal/browser-utils';
import * as utils from '@sentry/core';
Expand Down
2 changes: 1 addition & 1 deletion packages/browser/test/transports/offline.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, expect, it } from 'vitest';
import { describe, expect, it, beforeAll } from 'vitest';

import 'fake-indexeddb/auto';

Expand Down
2 changes: 1 addition & 1 deletion packages/browser/test/utils/featureFlags.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { FeatureFlag } from '@sentry/core';

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

describe('flags', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/test/utils-hoist/envelope.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
spanToJSON,
} from '@sentry/core';
import { SentrySpan } from '@sentry/core';
import { describe, expect, it, test, vi } from 'vitest';
import { describe, expect, it, test, vi, afterEach } from 'vitest';
import { getSentryCarrier } from '../../src/carrier';
import {
addItemToEnvelope,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { RuleTester } from 'eslint';
import { describe } from 'vitest';
import { describe, test } from 'vitest';

// @ts-expect-error untyped module
import rule from '../../../src/rules/no-eq-empty';
Expand Down
2 changes: 1 addition & 1 deletion packages/feedback/test/core/getFeedback.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { vi, describe, it, expect } from 'vitest';
import { vi, describe, it, expect, beforeEach } from 'vitest';

import { getCurrentScope } from '@sentry/core';
import { getFeedback } from '../../src/core/getFeedback';
Expand Down
2 changes: 1 addition & 1 deletion packages/feedback/test/core/sendFeedback.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @vitest-environment jsdom
*/
import { vi, describe, it, expect } from 'vitest';
import { vi, describe, it, expect, beforeEach, afterAll } from 'vitest';

import {
addBreadcrumb,
Expand Down
2 changes: 1 addition & 1 deletion packages/nestjs/test/integrations/nest.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { beforeEach, describe, expect, it, vi, afterEach } from 'vitest';

import * as core from '@sentry/core';
import { isPatched } from '../../src/integrations/helpers';
Expand Down
1 change: 1 addition & 0 deletions packages/react/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import baseConfig from '../../vite/vite.config';
export default defineConfig({
...baseConfig,
test: {
globals: true,
...baseConfig.test,
},
});
2 changes: 1 addition & 1 deletion packages/replay-internal/test.setup.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { printDiffOrStringify } from 'jest-matcher-utils';
import { vi } from 'vitest';
import { vi, expect } from 'vitest';
import type { Mocked, MockedFunction } from 'vitest';

/* eslint-disable @typescript-eslint/no-unsafe-member-access */
Expand Down
2 changes: 1 addition & 1 deletion packages/replay-internal/test/unit/util/logger.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { beforeEach, describe, expect, it } from 'vitest';
import { beforeEach, describe, expect, it, vi } from 'vitest';

import * as SentryCore from '@sentry/core';
import { logger as coreLogger } from '@sentry/core';
Expand Down
2 changes: 1 addition & 1 deletion packages/solidstart/test/client/errorboundary.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type * as SentryBrowser from '@sentry/browser';
import { createTransport, getCurrentScope, setCurrentClient } from '@sentry/core';
import { render } from '@solidjs/testing-library';
import userEvent from '@testing-library/user-event';
import { vi } from 'vitest';
import { vi, describe, beforeEach, afterEach, it, expect } from 'vitest';

import { ErrorBoundary } from 'solid-js';
import { BrowserClient, withSentryErrorBoundary } from '../../src/client';
Expand Down
2 changes: 1 addition & 1 deletion packages/solidstart/test/client/sdk.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { SDK_VERSION } from '@sentry/solid';
import * as SentrySolid from '@sentry/solid';

import { vi } from 'vitest';
import { vi, describe, beforeEach, it, expect } from 'vitest';
import { init as solidStartInit } from '../../src/client';
import { solidRouterBrowserTracingIntegration } from '../../src/client/solidrouter';

Expand Down
2 changes: 1 addition & 1 deletion packages/solidstart/test/client/solidrouter.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
import type { MemoryHistory } from '@solidjs/router';
import { MemoryRouter, Navigate, Route, createMemoryHistory } from '@solidjs/router';
import { render } from '@solidjs/testing-library';
import { vi } from 'vitest';
import { vi, describe, it, beforeEach, expect } from 'vitest';

import { BrowserClient } from '../../src/client';
import { solidRouterBrowserTracingIntegration, withSentryRouterRouting } from '../../src/client/solidrouter';
Expand Down
2 changes: 1 addition & 1 deletion packages/solidstart/test/server/errorboundary.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type * as SentryCore from '@sentry/core';
import { createTransport, getCurrentScope, setCurrentClient } from '@sentry/core';
import { render } from '@solidjs/testing-library';
import userEvent from '@testing-library/user-event';
import { vi } from 'vitest';
import { vi, describe, beforeEach, it, expect, afterEach } from 'vitest';

import { ErrorBoundary } from 'solid-js';
import { NodeClient, withSentryErrorBoundary } from '../../src/server';
Expand Down
2 changes: 1 addition & 1 deletion packages/solidstart/test/server/middleware.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as SentryCore from '@sentry/core';
import { beforeEach, describe, it, vi } from 'vitest';
import { beforeEach, describe, it, vi, expect } from 'vitest';
import { sentryBeforeResponseMiddleware } from '../../src/server';
import type { ResponseMiddlewareResponse } from '../../src/server';

Expand Down
2 changes: 1 addition & 1 deletion packages/solidstart/test/server/solidrouter.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { MemoryHistory } from '@solidjs/router';
import { MemoryRouter, Route, createMemoryHistory } from '@solidjs/router';
import { render } from '@solidjs/testing-library';
import { vi } from 'vitest';
import { vi, describe, expect, it } from 'vitest';

import { withSentryRouterRouting as withSentryClientRouterRouting } from '../../src/client/solidrouter';
import { withSentryRouterRouting as withSentryServerRouterRouting } from '../../src/server/solidrouter';
Expand Down
2 changes: 1 addition & 1 deletion packages/solidstart/test/vite/buildInstrumentation.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { UserConfig } from 'vite';
import { describe, expect, it, vi } from 'vitest';
import { describe, expect, it, vi, beforeEach } from 'vitest';
import { makeBuildInstrumentationFilePlugin } from '../../src/vite/buildInstrumentationFile';

const fsAccessMock = vi.fn();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { rewriteFramesIntegration } from '@sentry/browser';
import { basename } from '@sentry/core';
import type { Event, StackFrame } from '@sentry/core';
import { describe, expect, it } from 'vitest';

import { rewriteFramesIteratee } from '../../src/server-common/rewriteFramesIntegration';
import type { GlobalWithSentryValues } from '../../src/vite/injectGlobalValues';
Expand Down
1 change: 0 additions & 1 deletion vite/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ export default defineConfig({
__DEBUG_BUILD__: true,
},
test: {
globals: true,
coverage: {
enabled: true,
reportsDirectory: './coverage',
Expand Down

0 comments on commit 3ad018d

Please sign in to comment.