Skip to content

Commit

Permalink
test(astro): Switch to explicit vitest imports (#13093)
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 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.
  • Loading branch information
AbhiPrasad authored Jul 30, 2024
1 parent 71af30f commit b7e62c4
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 17 deletions.
15 changes: 8 additions & 7 deletions packages/astro/test/client/sdk.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { afterEach, describe, expect, it, vi } from 'vitest';

import type { BrowserClient } from '@sentry/browser';
import {
browserTracingIntegration,
Expand All @@ -8,9 +10,8 @@ import {
} from '@sentry/browser';
import * as SentryBrowser from '@sentry/browser';
import { SDK_VERSION, getClient } from '@sentry/browser';
import { vi } from 'vitest';

import { init } from '../../../astro/src/client/sdk';
import { init } from '../../src/client/sdk';

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

Expand Down Expand Up @@ -66,7 +67,7 @@ describe('Sentry client SDK', () => {
...tracingOptions,
});

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

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

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

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

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

init({
dsn: 'https://public@dsn.ingest.sentry.io/1337',
enableTracing: true,
});

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

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

delete globalThis.__SENTRY_TRACING__;
delete (globalThis as any).__SENTRY_TRACING__;
});

it('Overrides the automatically default browserTracingIntegration instance with a a user-provided browserTracingIntegration instance', () => {
Expand Down
8 changes: 3 additions & 5 deletions packages/astro/test/integration/index.files.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { vi } from 'vitest';
import { afterEach, describe, expect, it, vi } from 'vitest';

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

vi.mock('fs', async () => {
const actual = await vi.importActual('fs');
vi.mock('fs', async requireActual => {
return {
// @ts-expect-error - just mocking around
...actual,
...(await requireActual<any>()),
existsSync: vi.fn(p => p.endsWith('js')),
};
});
Expand Down
4 changes: 2 additions & 2 deletions packages/astro/test/integration/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { vi } from 'vitest';
import { afterEach, describe, expect, it, vi } from 'vitest';

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

Expand Down Expand Up @@ -294,7 +294,7 @@ describe('sentryAstro integration', () => {

it.each([{ output: 'static' }, { output: undefined }])(
"doesn't add middleware if in static mode (config %s)",
async config => {
async (config: any) => {
const integration = sentryAstro({});
const addMiddleware = vi.fn();
const updateConfig = vi.fn();
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/test/integration/middleware/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { vi } from 'vitest';
import { describe, expect, it, vi } from 'vitest';

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

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

import { buildClientSnippet, buildSdkInitFileImportSnippet, buildServerSnippet } from '../../src/integration/snippets';

const allSdkOptions = {
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/tsconfig.test.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@

"compilerOptions": {
// should include all types from `./tsconfig.json` plus types for all test frameworks used
"types": ["node", "vitest/globals"]
"types": ["node"]
}
}
1 change: 0 additions & 1 deletion packages/astro/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@ export default {
...baseConfig,
test: {
...baseConfig.test,
environment: 'jsdom',
},
};

0 comments on commit b7e62c4

Please sign in to comment.