-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathautoInstrument.test.ts
230 lines (213 loc) · 7.72 KB
/
autoInstrument.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { canWrapLoad, makeAutoInstrumentationPlugin } from '../../src/vite/autoInstrument';
const DEFAULT_CONTENT = `
export const load = () => {};
export const prerender = true;
`;
let fileContent: string | undefined;
vi.mock('fs', async () => {
const actual = await vi.importActual('fs');
return {
...actual,
promises: {
// @ts-expect-error this also exists, I promise!
...actual.promises,
readFile: vi.fn().mockImplementation(() => {
return fileContent || DEFAULT_CONTENT;
}),
},
existsSync: vi.fn().mockImplementation(id => {
if (id === '+page.virtual.ts') {
return false;
}
return true;
}),
};
});
vi.mock('rollup', () => {
return {
rollup: vi.fn().mockReturnValue({ generate: vi.fn().mockReturnValue({ output: ['transformed'] }) }),
};
});
describe('makeAutoInstrumentationPlugin()', () => {
beforeEach(() => {
vi.clearAllMocks();
fileContent = undefined;
});
it('returns the auto instrumentation plugin', async () => {
const plugin = makeAutoInstrumentationPlugin({ debug: true, load: true, serverLoad: true });
expect(plugin.name).toEqual('sentry-auto-instrumentation');
expect(plugin.enforce).toEqual('pre');
expect(plugin.load).toEqual(expect.any(Function));
});
describe.each([
'path/to/+page.ts',
'path/to/+page.js',
'path/to/+page.mts',
'path/to/+page.mjs',
'path/to/+layout.ts',
'path/to/+layout.js',
'path/to/+layout.mts',
'path/to/+layout.mjs',
])('transform %s files', (path: string) => {
it('wraps universal load if `load` option is `true`', async () => {
const plugin = makeAutoInstrumentationPlugin({ debug: false, load: true, serverLoad: true });
// @ts-expect-error this exists
const loadResult = await plugin.load(path);
expect(loadResult).toEqual(
'import { wrapLoadWithSentry } from "@sentry/sveltekit";' +
`import * as userModule from "${path}?sentry-auto-wrap";` +
'export const load = userModule.load ? wrapLoadWithSentry(userModule.load) : undefined;' +
`export * from "${path}?sentry-auto-wrap";`,
);
});
it("doesn't wrap universal load if `load` option is `false`", async () => {
const plugin = makeAutoInstrumentationPlugin({
debug: false,
load: false,
serverLoad: false,
});
// @ts-expect-error this exists
const loadResult = await plugin.load(path);
expect(loadResult).toEqual(null);
});
});
describe.each([
'path/to/+page.server.ts',
'path/to/+page.server.js',
'path/to/+page.server.mts',
'path/to/+page.server.mjs',
'path/to/+layout.server.ts',
'path/to/+layout.server.js',
'path/to/+layout.server.mts',
'path/to/+layout.server.mjs',
])('transform %s files', (path: string) => {
it('wraps universal load if `load` option is `true`', async () => {
const plugin = makeAutoInstrumentationPlugin({ debug: false, load: false, serverLoad: true });
// @ts-expect-error this exists
const loadResult = await plugin.load(path);
expect(loadResult).toEqual(
'import { wrapServerLoadWithSentry } from "@sentry/sveltekit";' +
`import * as userModule from "${path}?sentry-auto-wrap";` +
'export const load = userModule.load ? wrapServerLoadWithSentry(userModule.load) : undefined;' +
`export * from "${path}?sentry-auto-wrap";`,
);
});
it("doesn't wrap universal load if `load` option is `false`", async () => {
const plugin = makeAutoInstrumentationPlugin({
debug: false,
load: false,
serverLoad: false,
});
// @ts-expect-error this exists
const loadResult = await plugin.load(path);
expect(loadResult).toEqual(null);
});
});
});
describe('canWrapLoad', () => {
afterEach(() => {
fileContent = undefined;
});
it.each([
['export variable declaration - function pointer', 'export const load= loadPageData'],
['export variable declaration - factory function call', 'export const load =loadPageData()'],
['export variable declaration - inline function', 'export const load = () => { return { props: { msg: "hi" } } }'],
['export variable declaration - inline function let', 'export let load = () => {}'],
[
'export variable declaration - inline async function',
'export const load = async () => { return { props: { msg: "hi" } } }',
],
['export function declaration', 'export function load ( ){ return { props: { msg: "hi" } } }'],
[
'export function declaration - with params',
`export async function load({fetch}){
const res = await fetch('https://example.com');
return { props: { msg: res.toString() } }
}`,
],
[
'export function declaration - with angle bracket type assertion',
`export async function load() {
let x: unknown = 'foo';
return {
msg: <string>x,
};
}`,
],
[
'variable declaration (let)',
`import {something} from 'somewhere';
let load = async () => {};
export const prerender = true;
export { load}`,
],
[
'variable declaration (var)',
`import {something} from 'somewhere';
var load=async () => {};
export const prerender = true;
export { load}`,
],
[
'function declaration',
`import {something} from 'somewhere';
async function load(){};
export { load }`,
],
[
'function declaration, sentry commented out',
`import {something} from 'somewhere';
// import * as Sentry from '@sentry/sveltekit';
async function load(){};
export { load }`,
],
[
'function declaration, sentry commented out',
`import {something} from 'somewhere';
/* import * as Sentry from '@sentry/sveltekit'; */
async function load(){};
export { load }`,
],
[
'function declaration with different name',
`import { foo } from 'somewhere';
async function somethingElse(){};
export { somethingElse as load, foo }`,
],
[
'function declaration with different string literal name',
`import { foo } from 'somewhere';
async function somethingElse(){};
export { somethingElse as "load", foo }`,
],
[
'export variable declaration - inline function with assigned type',
`import type { LayoutLoad } from './$types';
export const load : LayoutLoad = async () => { return { props: { msg: "hi" } } }`,
],
])('returns `true` if a load declaration (%s) exists', async (_, code) => {
fileContent = code;
expect(await canWrapLoad('+page.ts', false)).toEqual(true);
});
it.each([
'export const almostLoad = () => {}; export const prerender = true;',
'export const loadNotLoad = () => {}; export const prerender = true;',
'export function aload(){}; export const prerender = true;',
'export function loader(){}; export const prerender = true;',
'let loadme = false; export {loadme}',
'const a = {load: true}; export {a}',
'if (s === "load") {}',
'const a = load ? load : false',
'// const load = () => {}',
'/* export const load = () => {} */ export const prerender = true;',
'/* export const notLoad = () => { const a = getSomething() as load; } */ export const prerender = true;',
])('returns `false` if no load declaration exists', async code => {
fileContent = code;
expect(await canWrapLoad('+page.ts', false)).toEqual(false);
});
it("returns `false` if the passed file id doesn't exist", async () => {
fileContent = DEFAULT_CONTENT;
expect(await canWrapLoad('+page.virtual.ts', false)).toEqual(false);
});
});