Skip to content

Commit

Permalink
test(replay): Switch to explicit vitest imports (#13027)
Browse files Browse the repository at this point in the history
As per https://vitest.dev/config/#globals

> By default, vitest does not provide global APIs for explicitness

I think we should follow vitest defaults here and explicitly import in
the APIs that we need. This refactors our Replay SDK tests to do so.

This change also removes `environment: 'jsdom'` from the vite config in
favour of explicitly adding jsdom environment via the
`@vitest-environment` pragma to the specific test file that needs it.
This should means that our non-browser tests are not polluted with jsdom
globals, and that future writers have to explicitly opt-in to the
behaviour.
  • Loading branch information
AbhiPrasad authored Jul 24, 2024
1 parent d570c19 commit 92ce821
Show file tree
Hide file tree
Showing 55 changed files with 245 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { vi } from 'vitest';
/**
* @vitest-environment jsdom
*/

import { afterEach, describe, expect, test, vi } from 'vitest';

import { EventType } from '@sentry-internal/rrweb';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { vi } from 'vitest';
/**
* @vitest-environment jsdom
*/

import type { MockInstance, MockedFunction } from 'vitest';
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';

import * as SentryBrowserUtils from '@sentry-internal/browser-utils';
import * as SentryCore from '@sentry/core';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { vi } from 'vitest';
/**
* @vitest-environment jsdom
*/

import type { MockInstance } from 'vitest';
import { afterEach, describe, expect, it, vi } from 'vitest';

import { useFakeTimers } from '../../utils/use-fake-timers';

useFakeTimers();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { vi } from 'vitest';
/**
* @vitest-environment jsdom
*/

import { afterEach, describe, expect, it, vi } from 'vitest';

import { handleBeforeSendEvent } from '../../../src/coreHandlers/handleBeforeSendEvent';
import type { ReplayContainer } from '../../../src/replay';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/**
* @vitest-environment jsdom
*/

import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';

import { getClient } from '@sentry/core';
import type { Event } from '@sentry/types';

Expand Down
6 changes: 6 additions & 0 deletions packages/replay-internal/test/integration/earlyEvents.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/**
* @vitest-environment jsdom
*/

import { beforeEach, describe, expect, it, vi } from 'vitest';

import { BASE_TIMESTAMP } from '..';
import { resetSdkMock } from '../mocks/resetSdkMock';
import { useFakeTimers } from '../utils/use-fake-timers';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
/**
* @vitest-environment jsdom
*/

import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';

import { captureException, getClient } from '@sentry/core';
import { vi } from 'vitest';

import {
BUFFER_CHECKOUT_TIME,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { vi } from 'vitest';
/**
* @vitest-environment jsdom
*/

import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';

import { getClient, getCurrentScope } from '@sentry/core';
import type { Event } from '@sentry/types';
Expand Down
7 changes: 7 additions & 0 deletions packages/replay-internal/test/integration/events.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/**
* @vitest-environment jsdom
*/

import type { MockInstance } from 'vitest';
import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';

import { getClient } from '@sentry/core';

import { WINDOW } from '../../src/constants';
Expand Down
6 changes: 5 additions & 1 deletion packages/replay-internal/test/integration/flush.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { vi } from 'vitest';
/**
* @vitest-environment jsdom
*/

import type { MockedFunction } from 'vitest';
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';

import { useFakeTimers } from '../utils/use-fake-timers';

Expand Down
6 changes: 6 additions & 0 deletions packages/replay-internal/test/integration/getReplayId.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/**
* @vitest-environment jsdom
*/

import { afterEach, describe, expect, it, vi } from 'vitest';

import { mockSdk } from '../mocks/mockSdk';
import { useFakeTimers } from '../utils/use-fake-timers';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { vi } from 'vitest';
/**
* @vitest-environment jsdom
*/

import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';

import type { MockInstance } from 'vitest';

import { mockSdk } from '../index';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/**
* @vitest-environment jsdom
*/

import type { MockedFunction } from 'vitest';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';

import { getClient } from '@sentry/core';
import type { Transport, TransportMakeRequestResponse } from '@sentry/types';

Expand All @@ -14,7 +21,7 @@ async function advanceTimers(time: number) {
await new Promise(process.nextTick);
}

type MockTransportSend = vi.MockedFunction<Transport['send']>;
type MockTransportSend = MockedFunction<Transport['send']>;

describe('Integration | rate-limiting behaviour', () => {
let replay: ReplayContainer;
Expand Down
8 changes: 7 additions & 1 deletion packages/replay-internal/test/integration/rrweb.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/**
* @vitest-environment jsdom
*/

import { afterEach, describe, expect, it, vi } from 'vitest';

import { resetSdkMock } from '../mocks/resetSdkMock';
import { useFakeTimers } from '../utils/use-fake-timers';

Expand All @@ -15,7 +21,7 @@ describe('Integration | rrweb', () => {
stickySession: false,
},
});
expect(mockRecord.mock.calls[0][0]).toMatchInlineSnapshot(`
expect(mockRecord.mock.calls[0]?.[0]).toMatchInlineSnapshot(`
{
"blockSelector": ".sentry-block,[data-sentry-block],base[href="/"],img,image,svg,video,object,picture,embed,map,audio,link[rel="icon"],link[rel="apple-touch-icon"]",
"collectFonts": true,
Expand Down
6 changes: 6 additions & 0 deletions packages/replay-internal/test/integration/sampling.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/**
* @vitest-environment jsdom
*/

import { beforeEach, describe, expect, it, vi } from 'vitest';

import { getClient } from '@sentry/core';
import { resetSdkMock } from '../mocks/resetSdkMock';
import { useFakeTimers } from '../utils/use-fake-timers';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { vi } from 'vitest';
/**
* @vitest-environment jsdom
*/

import type { MockInstance, MockedFunction } from 'vitest';
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';

import * as SentryBrowserUtils from '@sentry-internal/browser-utils';
import * as SentryCore from '@sentry/core';
Expand Down
6 changes: 5 additions & 1 deletion packages/replay-internal/test/integration/session.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { vi } from 'vitest';
/**
* @vitest-environment jsdom
*/

import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';

import { getClient } from '@sentry/core';
import type { Transport } from '@sentry/types';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/**
* @vitest-environment jsdom
*/

import { beforeEach, describe, expect, it, vi } from 'vitest';

import { shouldFilterRequest } from '../../src/util/shouldFilterRequest';
import { mockSdk } from '../index';

Expand Down
6 changes: 5 additions & 1 deletion packages/replay-internal/test/integration/start.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { vi } from 'vitest';
/**
* @vitest-environment jsdom
*/

import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';

import { getClient } from '@sentry/core';
import type { Transport } from '@sentry/types';
Expand Down
6 changes: 5 additions & 1 deletion packages/replay-internal/test/integration/stop.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { vi } from 'vitest';
/**
* @vitest-environment jsdom
*/

import type { MockInstance, MockedFunction } from 'vitest';
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';

import * as SentryBrowserUtils from '@sentry-internal/browser-utils';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { describe, expect, it } from 'vitest';

import { CONSOLE_ARG_MAX_SIZE } from '../../../src/constants';
import { normalizeBreadcrumb, normalizeConsoleBreadcrumb } from '../../../src/coreHandlers/handleBreadcrumbs';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/**
* @vitest-environment jsdom
*/

import { beforeEach, describe, expect, it, test, vi } from 'vitest';

import { useFakeTimers } from '../../utils/use-fake-timers';

useFakeTimers();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/**
* @vitest-environment jsdom
*/

import { describe, expect, test } from 'vitest';

import type { HandlerDataDom } from '@sentry/types';

import { handleDom } from '../../../src/coreHandlers/handleDom';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/**
* @vitest-environment jsdom
*/

import { describe, expect, it } from 'vitest';

import { getKeyboardBreadcrumb } from '../../../src/coreHandlers/handleKeyboardEvent';

describe('Unit | coreHandlers | handleKeyboardEvent', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/**
* @vitest-environment jsdom
*/

import { beforeEach, describe, expect, it, vi } from 'vitest';

import { SENTRY_XHR_DATA_KEY } from '@sentry-internal/browser-utils';
import type {
Breadcrumb,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/**
* @vitest-environment jsdom
*/

import { beforeEach, describe, expect, it, vi } from 'vitest';

import { BASE_TIMESTAMP } from '../../..';
import { addBreadcrumbEvent } from '../../../../src/coreHandlers/util/addBreadcrumbEvent';
import type { EventBufferArray } from '../../../../src/eventBuffer/EventBufferArray';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { describe, expect, it, vi } from 'vitest';

import { useFakeTimers } from '../../../utils/use-fake-timers';

useFakeTimers();

import { vi } from 'vitest';

import { _getResponseInfo } from '../../../../src/coreHandlers/util/fetchUtils';

describe('Unit | coreHandlers | util | fetchUtils', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { expect, it } from 'vitest';

import { getAttributesToRecord } from '../../../../src/coreHandlers/util/getAttributesToRecord';

it('records only included attributes', function () {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/**
* @vitest-environment jsdom
*/

import { describe, expect, it } from 'vitest';

import { NETWORK_BODY_MAX_SIZE } from '../../../../src/constants';
import {
buildNetworkRequestOrResponse,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/**
* @vitest-environment jsdom
*/

import { describe, expect, it } from 'vitest';

import { _parseXhrResponse } from '../../../../src/coreHandlers/util/xhrUtils';

describe('Unit | coreHandlers | util | xhrUtils', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { describe, expect, it } from 'vitest';

import { REPLAY_MAX_EVENT_BUFFER_SIZE } from '../../../src/constants';
import { createEventBuffer } from '../../../src/eventBuffer';
import { EventBufferSizeExceededError } from '../../../src/eventBuffer/error';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
/**
* @vitest-environment jsdom
*/

import 'jsdom-worker';

import { describe, expect, it, vi } from 'vitest';

import { BASE_TIMESTAMP } from '../..';
import { REPLAY_MAX_EVENT_BUFFER_SIZE } from '../../../src/constants';
import { createEventBuffer } from '../../../src/eventBuffer';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
/**
* @vitest-environment jsdom
*/

import 'jsdom-worker';

import type { MockInstance } from 'vitest';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';

import { BASE_TIMESTAMP } from '../..';
import { EventBufferProxy } from '../../../src/eventBuffer/EventBufferProxy';
import { decompress } from '../../utils/compression';
Expand Down
2 changes: 2 additions & 0 deletions packages/replay-internal/test/unit/multipleInstances.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { describe, expect, it } from 'vitest';

import { replayIntegration } from '../../src/integration';

describe('Unit | multipleInstances', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { vi } from 'vitest';
/**
* @vitest-environment jsdom
*/

import type { MockedFunction } from 'vitest';
import { afterEach, beforeAll, describe, expect, it, vi } from 'vitest';

import * as Sentry from '@sentry/core';

Expand All @@ -16,7 +21,7 @@ vi.mock('@sentry/utils', async () => {
};
});

type CaptureEventMockType = vi.MockedFunction<typeof Sentry.captureEvent>;
type CaptureEventMockType = MockedFunction<typeof Sentry.captureEvent>;

describe('Unit | session | createSession', () => {
const captureEventMock: CaptureEventMockType = vi.fn();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/**
* @vitest-environment jsdom
*/

import { afterEach, beforeAll, describe, expect, it } from 'vitest';

import { REPLAY_SESSION_KEY, WINDOW } from '../../../src/constants';
import { fetchSession } from '../../../src/session/fetchSession';

Expand Down
Loading

0 comments on commit 92ce821

Please sign in to comment.