Skip to content

Commit 59c0e84

Browse files
committed
add tests
1 parent 8c7c821 commit 59c0e84

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed

packages/svelte/test/config.test.ts

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import { withSentryConfig, SvelteConfig, SentrySvelteConfigOptions } from '../src/config';
2+
import { componentTrackingPreprocessor, FIRST_PASS_COMPONENT_TRACKING_PREPROC_ID } from '../src/preprocessors';
3+
import { SentryPreprocessorGroup } from '../src/types';
4+
5+
describe('withSentryConfig', () => {
6+
it.each([
7+
[
8+
'no preprocessors specified',
9+
{
10+
compilerOptions: {
11+
enableSourcemap: true,
12+
},
13+
},
14+
],
15+
[
16+
'a single preprocessor specified',
17+
{
18+
compilerOptions: {
19+
enableSourcemap: true,
20+
},
21+
preprocess: {},
22+
},
23+
],
24+
[
25+
'an array of preprocessors specified',
26+
{
27+
compilerOptions: {
28+
enableSourcemap: true,
29+
},
30+
preprocess: [{}, {}, {}],
31+
},
32+
],
33+
])('adds our preprocessors by default to the provided svelte config with %s', (_, originalConfig: SvelteConfig) => {
34+
const wrappedConfig = withSentryConfig(originalConfig);
35+
36+
const originalPreprocs = originalConfig.preprocess;
37+
const originalNumberOfPreprocs = originalPreprocs
38+
? Array.isArray(originalPreprocs)
39+
? originalPreprocs.length
40+
: 1
41+
: 0;
42+
43+
expect(Array.isArray(wrappedConfig.preprocess)).toBe(true);
44+
expect(wrappedConfig).toEqual({ ...originalConfig, preprocess: expect.any(Array) });
45+
expect(wrappedConfig.preprocess).toHaveLength(originalNumberOfPreprocs + 1);
46+
expect((wrappedConfig.preprocess as SentryPreprocessorGroup[])[0].id).toEqual(
47+
FIRST_PASS_COMPONENT_TRACKING_PREPROC_ID,
48+
);
49+
});
50+
51+
it("doesn't add Sentry preprocessors that were already added by the users", () => {
52+
const originalConfig = {
53+
compilerOptions: {
54+
enableSourcemap: true,
55+
},
56+
preprocess: componentTrackingPreprocessor(),
57+
};
58+
59+
const wrappedConfig = withSentryConfig(originalConfig);
60+
61+
expect(wrappedConfig).toEqual({ ...originalConfig, preprocess: expect.any(Array) });
62+
expect(wrappedConfig.preprocess).toHaveLength(1);
63+
});
64+
65+
it('handles multiple wraps correctly by only adding our preprocessors once', () => {
66+
const originalConfig = {
67+
compilerOptions: {
68+
enableSourcemap: true,
69+
},
70+
};
71+
72+
const wrappedConfig = withSentryConfig(withSentryConfig(withSentryConfig(originalConfig)));
73+
74+
expect(wrappedConfig).toEqual({ ...originalConfig, preprocess: expect.any(Array) });
75+
expect(wrappedConfig.preprocess).toHaveLength(1);
76+
});
77+
78+
it("doesn't add component tracking preprocessors if the feature is deactivated", () => {
79+
const originalConfig = {
80+
compilerOptions: {
81+
enableSourcemap: true,
82+
},
83+
preprocess: [{}],
84+
};
85+
86+
const sentryOptions: SentrySvelteConfigOptions = { componentTracking: { trackComponents: false } };
87+
const wrappedConfig = withSentryConfig(originalConfig, sentryOptions);
88+
89+
expect(wrappedConfig).toEqual(originalConfig);
90+
});
91+
});

0 commit comments

Comments
 (0)