Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mydea committed Jan 19, 2024
1 parent c298879 commit fbdd611
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 103 deletions.
2 changes: 1 addition & 1 deletion packages/sveltekit/src/client/browserTracingIntegration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { BrowserTracing as OriginalBrowserTracing } from '@sentry/svelte';
import { svelteKitRoutingInstrumentation } from './router';

/**
* A custom BrowserTracing integration for Next.js.
* A custom BrowserTracing integration for Sveltekit.
*/
export class BrowserTracing extends OriginalBrowserTracing {
public constructor(options?: ConstructorParameters<typeof OriginalBrowserTracing>[0]) {
Expand Down
4 changes: 3 additions & 1 deletion packages/sveltekit/src/server/rewriteFramesIntegration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ export const rewriteFramesIntegration = defineIntegration(customRewriteFramesInt
* module name to the original file name correctly, leading to individual error groups for
* each module. Removing the `module` field makes the grouping algorithm fall back to the
* `filename` field, which is correctly resolved and hence grouping works as expected.
*
* Exported for tests only.
*/
function rewriteFramesIteratee(frame: StackFrame): StackFrame {
export function rewriteFramesIteratee(frame: StackFrame): StackFrame {
if (!frame.filename) {
return frame;
}
Expand Down
12 changes: 0 additions & 12 deletions packages/sveltekit/test/client/sdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,7 @@ describe('Sentry client SDK', () => {
...tracingOptions,
});

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

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

Expand All @@ -77,10 +74,7 @@ describe('Sentry client SDK', () => {
...tracingOptions,
});

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

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

Expand All @@ -96,10 +90,7 @@ describe('Sentry client SDK', () => {
enableTracing: true,
});

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

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

// @ts-expect-error this is fine in the test
Expand All @@ -113,12 +104,9 @@ describe('Sentry client SDK', () => {
enableTracing: true,
});

const integrationsToInit = svelteInit.mock.calls[0][0].integrations;

const browserTracing = getClient<BrowserClient>()?.getIntegrationByName('BrowserTracing') as BrowserTracing;
const options = browserTracing.options;

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

// This shows that the user-configured options are still here
Expand Down
88 changes: 88 additions & 0 deletions packages/sveltekit/test/server/rewriteFramesIntegration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { RewriteFrames } from '@sentry/integrations';
import type { Event, StackFrame } from '@sentry/types';
import { basename } from '@sentry/utils';

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

describe('rewriteFramesIteratee', () => {
it('removes the module property from the frame', () => {
const frame: StackFrame = {
filename: '/some/path/to/server/chunks/3-ab34d22f.js',
module: '3-ab34d22f.js',
};

const result = rewriteFramesIteratee(frame);

expect(result).not.toHaveProperty('module');
});

it('does the same filename modification as the default RewriteFrames iteratee if no output dir is available', () => {
const frame: StackFrame = {
filename: '/some/path/to/server/chunks/3-ab34d22f.js',
lineno: 1,
colno: 1,
module: '3-ab34d22f.js',
};

// eslint-disable-next-line deprecation/deprecation
const originalRewriteFrames = new RewriteFrames();
// eslint-disable-next-line deprecation/deprecation
const rewriteFrames = new RewriteFrames({ iteratee: rewriteFramesIteratee });

const event: Event = {
exception: {
values: [
{
stacktrace: {
frames: [frame],
},
},
],
},
};

const originalResult = originalRewriteFrames.processEvent(event);
const result = rewriteFrames.processEvent(event);

expect(result.exception?.values?.[0]?.stacktrace?.frames?.[0]).toEqual({
filename: 'app:///3-ab34d22f.js',
lineno: 1,
colno: 1,
});

expect(result).toStrictEqual(originalResult);
});

it.each([
['adapter-node', 'build', '/absolute/path/to/build/server/chunks/3-ab34d22f.js', 'app:///chunks/3-ab34d22f.js'],
[
'adapter-auto',
'.svelte-kit/output',
'/absolute/path/to/.svelte-kit/output/server/entries/pages/page.ts.js',
'app:///entries/pages/page.ts.js',
],
])(
'removes the absolut path to the server output dir, if the output dir is available (%s)',
(_, outputDir, frameFilename, modifiedFilename) => {
(globalThis as unknown as GlobalWithSentryValues).__sentry_sveltekit_output_dir = outputDir;

const frame: StackFrame = {
filename: frameFilename,
lineno: 1,
colno: 1,
module: basename(frameFilename),
};

const result = rewriteFramesIteratee({ ...frame });

expect(result).toStrictEqual({
filename: modifiedFilename,
lineno: 1,
colno: 1,
});

delete (globalThis as unknown as GlobalWithSentryValues).__sentry_sveltekit_output_dir;
},
);
});
12 changes: 11 additions & 1 deletion packages/sveltekit/test/server/sdk.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as SentryNode from '@sentry/node';
import { SDK_VERSION } from '@sentry/node';
import type { NodeClient} from '@sentry/node';
import { SDK_VERSION, getClient } from '@sentry/node';
import { GLOBAL_OBJ } from '@sentry/utils';

import { init } from '../../src/server/sdk';
Expand Down Expand Up @@ -46,5 +47,14 @@ describe('Sentry server SDK', () => {
// @ts-expect-error need access to protected _tags attribute
expect(currentScope._tags).toEqual({ runtime: 'node' });
});

it('adds rewriteFramesIntegration by default', () => {
init({
dsn: 'https://public@dsn.ingest.sentry.io/1337',
});

const rewriteFramesIntegration = getClient<NodeClient>()?.getIntegrationByName('RewriteFrames');
expect(rewriteFramesIntegration).toBeDefined();
});
});
});
89 changes: 1 addition & 88 deletions packages/sveltekit/test/server/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import { RewriteFrames } from '@sentry/integrations';
import type { Event, StackFrame } from '@sentry/types';
import { basename } from '@sentry/utils';

import type { GlobalWithSentryValues } from '../../src/server/utils';
import { getTracePropagationData, rewriteFramesIteratee } from '../../src/server/utils';
import { getTracePropagationData } from '../../src/server/utils';

const MOCK_REQUEST_EVENT: any = {
request: {
Expand Down Expand Up @@ -58,85 +53,3 @@ describe('getTracePropagationData', () => {
expect(dynamicSamplingContext).toBeUndefined();
});
});

describe('rewriteFramesIteratee', () => {
it('removes the module property from the frame', () => {
const frame: StackFrame = {
filename: '/some/path/to/server/chunks/3-ab34d22f.js',
module: '3-ab34d22f.js',
};

const result = rewriteFramesIteratee(frame);

expect(result).not.toHaveProperty('module');
});

it('does the same filename modification as the default RewriteFrames iteratee if no output dir is available', () => {
const frame: StackFrame = {
filename: '/some/path/to/server/chunks/3-ab34d22f.js',
lineno: 1,
colno: 1,
module: '3-ab34d22f.js',
};

// eslint-disable-next-line deprecation/deprecation
const originalRewriteFrames = new RewriteFrames();
// eslint-disable-next-line deprecation/deprecation
const rewriteFrames = new RewriteFrames({ iteratee: rewriteFramesIteratee });

const event: Event = {
exception: {
values: [
{
stacktrace: {
frames: [frame],
},
},
],
},
};

const originalResult = originalRewriteFrames.processEvent(event);
const result = rewriteFrames.processEvent(event);

expect(result.exception?.values?.[0]?.stacktrace?.frames?.[0]).toEqual({
filename: 'app:///3-ab34d22f.js',
lineno: 1,
colno: 1,
});

expect(result).toStrictEqual(originalResult);
});

it.each([
['adapter-node', 'build', '/absolute/path/to/build/server/chunks/3-ab34d22f.js', 'app:///chunks/3-ab34d22f.js'],
[
'adapter-auto',
'.svelte-kit/output',
'/absolute/path/to/.svelte-kit/output/server/entries/pages/page.ts.js',
'app:///entries/pages/page.ts.js',
],
])(
'removes the absolut path to the server output dir, if the output dir is available (%s)',
(_, outputDir, frameFilename, modifiedFilename) => {
(globalThis as GlobalWithSentryValues).__sentry_sveltekit_output_dir = outputDir;

const frame: StackFrame = {
filename: frameFilename,
lineno: 1,
colno: 1,
module: basename(frameFilename),
};

const result = rewriteFramesIteratee({ ...frame });

expect(result).toStrictEqual({
filename: modifiedFilename,
lineno: 1,
colno: 1,
});

delete (globalThis as GlobalWithSentryValues).__sentry_sveltekit_output_dir;
},
);
});

0 comments on commit fbdd611

Please sign in to comment.