Skip to content

Commit b7e62c4

Browse files
authored
test(astro): Switch to explicit vitest imports (#13093)
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 Astro SDK tests to do so. I also went ahead and fixed up some TS errors in some tests. This change also removes `environment: 'jsdom'` from the vite config as it seems nothing needs this for astro. This should means that our tests are not polluted with jsdom globals, and that future writers have to explicitly opt-in to the behaviour.
1 parent 71af30f commit b7e62c4

File tree

7 files changed

+17
-17
lines changed

7 files changed

+17
-17
lines changed

packages/astro/test/client/sdk.test.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { afterEach, describe, expect, it, vi } from 'vitest';
2+
13
import type { BrowserClient } from '@sentry/browser';
24
import {
35
browserTracingIntegration,
@@ -8,9 +10,8 @@ import {
810
} from '@sentry/browser';
911
import * as SentryBrowser from '@sentry/browser';
1012
import { SDK_VERSION, getClient } from '@sentry/browser';
11-
import { vi } from 'vitest';
1213

13-
import { init } from '../../../astro/src/client/sdk';
14+
import { init } from '../../src/client/sdk';
1415

1516
const browserInit = vi.spyOn(SentryBrowser, 'init');
1617

@@ -66,7 +67,7 @@ describe('Sentry client SDK', () => {
6667
...tracingOptions,
6768
});
6869

69-
const integrationsToInit = browserInit.mock.calls[0][0]?.defaultIntegrations;
70+
const integrationsToInit = browserInit.mock.calls[0]![0]?.defaultIntegrations;
7071
const browserTracing = getClient<BrowserClient>()?.getIntegrationByName('BrowserTracing');
7172

7273
expect(integrationsToInit).toContainEqual(expect.objectContaining({ name: 'BrowserTracing' }));
@@ -82,28 +83,28 @@ describe('Sentry client SDK', () => {
8283
...tracingOptions,
8384
});
8485

85-
const integrationsToInit = browserInit.mock.calls[0][0]?.defaultIntegrations || [];
86+
const integrationsToInit = browserInit.mock.calls[0]![0]?.defaultIntegrations || [];
8687
const browserTracing = getClient<BrowserClient>()?.getIntegrationByName('BrowserTracing');
8788

8889
expect(integrationsToInit).not.toContainEqual(expect.objectContaining({ name: 'BrowserTracing' }));
8990
expect(browserTracing).toBeUndefined();
9091
});
9192

9293
it("doesn't add browserTracingIntegration if `__SENTRY_TRACING__` is set to false", () => {
93-
globalThis.__SENTRY_TRACING__ = false;
94+
(globalThis as any).__SENTRY_TRACING__ = false;
9495

9596
init({
9697
dsn: 'https://[email protected]/1337',
9798
enableTracing: true,
9899
});
99100

100-
const integrationsToInit = browserInit.mock.calls[0][0]?.defaultIntegrations || [];
101+
const integrationsToInit = browserInit.mock.calls[0]![0]?.defaultIntegrations || [];
101102
const browserTracing = getClient<BrowserClient>()?.getIntegrationByName('BrowserTracing');
102103

103104
expect(integrationsToInit).not.toContainEqual(expect.objectContaining({ name: 'BrowserTracing' }));
104105
expect(browserTracing).toBeUndefined();
105106

106-
delete globalThis.__SENTRY_TRACING__;
107+
delete (globalThis as any).__SENTRY_TRACING__;
107108
});
108109

109110
it('Overrides the automatically default browserTracingIntegration instance with a a user-provided browserTracingIntegration instance', () => {

packages/astro/test/integration/index.files.test.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
import { vi } from 'vitest';
1+
import { afterEach, describe, expect, it, vi } from 'vitest';
22

33
import { sentryAstro } from '../../src/integration';
44

5-
vi.mock('fs', async () => {
6-
const actual = await vi.importActual('fs');
5+
vi.mock('fs', async requireActual => {
76
return {
8-
// @ts-expect-error - just mocking around
9-
...actual,
7+
...(await requireActual<any>()),
108
existsSync: vi.fn(p => p.endsWith('js')),
119
};
1210
});

packages/astro/test/integration/index.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { vi } from 'vitest';
1+
import { afterEach, describe, expect, it, vi } from 'vitest';
22

33
import { sentryAstro } from '../../src/integration';
44

@@ -294,7 +294,7 @@ describe('sentryAstro integration', () => {
294294

295295
it.each([{ output: 'static' }, { output: undefined }])(
296296
"doesn't add middleware if in static mode (config %s)",
297-
async config => {
297+
async (config: any) => {
298298
const integration = sentryAstro({});
299299
const addMiddleware = vi.fn();
300300
const updateConfig = vi.fn();

packages/astro/test/integration/middleware/index.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { vi } from 'vitest';
1+
import { describe, expect, it, vi } from 'vitest';
22

33
import { onRequest } from '../../../src/integration/middleware';
44

packages/astro/test/integration/snippets.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { describe, expect, it } from 'vitest';
2+
13
import { buildClientSnippet, buildSdkInitFileImportSnippet, buildServerSnippet } from '../../src/integration/snippets';
24

35
const allSdkOptions = {

packages/astro/tsconfig.test.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55

66
"compilerOptions": {
77
// should include all types from `./tsconfig.json` plus types for all test frameworks used
8-
"types": ["node", "vitest/globals"]
8+
"types": ["node"]
99
}
1010
}

packages/astro/vite.config.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,5 @@ export default {
44
...baseConfig,
55
test: {
66
...baseConfig.test,
7-
environment: 'jsdom',
87
},
98
};

0 commit comments

Comments
 (0)