-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathutils.test.ts
129 lines (111 loc) · 4.55 KB
/
utils.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
import * as fs from 'fs';
import { afterEach, describe, expect, it, vi } from 'vitest';
import {
QUERY_END_INDICATOR,
SENTRY_FUNCTIONS_REEXPORT,
SENTRY_WRAPPED_ENTRY,
constructFunctionReExport,
extractFunctionReexportQueryParameters,
findDefaultSdkInitFile,
removeSentryQueryFromPath,
} from '../../src/vite/utils';
vi.mock('fs');
describe('findDefaultSdkInitFile', () => {
afterEach(() => {
vi.clearAllMocks();
});
it.each(['ts', 'js', 'mjs', 'cjs', 'mts', 'cts'])(
'should return the server file path with .%s extension if it exists',
ext => {
vi.spyOn(fs, 'existsSync').mockImplementation(filePath => {
return !(filePath instanceof URL) && filePath.includes(`sentry.server.config.${ext}`);
});
const result = findDefaultSdkInitFile('server');
expect(result).toMatch(`packages/nuxt/sentry.server.config.${ext}`);
},
);
it.each(['ts', 'js', 'mjs', 'cjs', 'mts', 'cts'])(
'should return the client file path with .%s extension if it exists',
ext => {
vi.spyOn(fs, 'existsSync').mockImplementation(filePath => {
return !(filePath instanceof URL) && filePath.includes(`sentry.client.config.${ext}`);
});
const result = findDefaultSdkInitFile('client');
expect(result).toMatch(`packages/nuxt/sentry.client.config.${ext}`);
},
);
it('should return undefined if no file with specified extensions exists', () => {
vi.spyOn(fs, 'existsSync').mockReturnValue(false);
const result = findDefaultSdkInitFile('server');
expect(result).toBeUndefined();
});
it('should return undefined if no file exists', () => {
vi.spyOn(fs, 'existsSync').mockReturnValue(false);
const result = findDefaultSdkInitFile('server');
expect(result).toBeUndefined();
});
it('should return the server config file path if server.config and instrument exist', () => {
vi.spyOn(fs, 'existsSync').mockImplementation(filePath => {
return (
!(filePath instanceof URL) &&
(filePath.includes('sentry.server.config.js') || filePath.includes('instrument.server.js'))
);
});
const result = findDefaultSdkInitFile('server');
expect(result).toMatch('packages/nuxt/sentry.server.config.js');
});
});
describe('removeSentryQueryFromPath', () => {
it('strips the Sentry query part from the path', () => {
const url = `/example/path${SENTRY_WRAPPED_ENTRY}${SENTRY_FUNCTIONS_REEXPORT}foo,${QUERY_END_INDICATOR}`;
const result = removeSentryQueryFromPath(url);
expect(result).toBe('/example/path');
});
it('returns the same path if the specific query part is not present', () => {
const url = '/example/path?other-query=param';
const result = removeSentryQueryFromPath(url);
expect(result).toBe(url);
});
});
describe('extractFunctionReexportQueryParameters', () => {
it.each([
[`${SENTRY_FUNCTIONS_REEXPORT}foo,bar,${QUERY_END_INDICATOR}`, ['foo', 'bar']],
[`${SENTRY_FUNCTIONS_REEXPORT}foo,bar,default${QUERY_END_INDICATOR}`, ['foo', 'bar']],
[
`${SENTRY_FUNCTIONS_REEXPORT}foo,a.b*c?d[e]f(g)h|i\\\\j(){hello},${QUERY_END_INDICATOR}`,
['foo', 'a\\.b\\*c\\?d\\[e\\]f\\(g\\)h\\|i\\\\\\\\j\\(\\)\\{hello\\}'],
],
[`/example/path/${SENTRY_FUNCTIONS_REEXPORT}foo,bar${QUERY_END_INDICATOR}`, ['foo', 'bar']],
[`${SENTRY_FUNCTIONS_REEXPORT}${QUERY_END_INDICATOR}`, []],
['?other-query=param', []],
])('extracts parameters from the query string: %s', (query, expected) => {
const result = extractFunctionReexportQueryParameters(query);
expect(result).toEqual(expected);
});
});
describe('constructFunctionReExport', () => {
it('constructs re-export code for given query parameters and entry ID', () => {
const query = `${SENTRY_FUNCTIONS_REEXPORT}foo,bar,${QUERY_END_INDICATOR}}`;
const query2 = `${SENTRY_FUNCTIONS_REEXPORT}foo,bar${QUERY_END_INDICATOR}}`;
const entryId = './module';
const result = constructFunctionReExport(query, entryId);
const result2 = constructFunctionReExport(query2, entryId);
const expected = `
export async function foo(...args) {
const res = await import("./module");
return res.foo.call(this, ...args);
}
export async function bar(...args) {
const res = await import("./module");
return res.bar.call(this, ...args);
}`;
expect(result.trim()).toBe(expected.trim());
expect(result2.trim()).toBe(expected.trim());
});
it('returns an empty string if the query string is empty', () => {
const query = '';
const entryId = './module';
const result = constructFunctionReExport(query, entryId);
expect(result).toBe('');
});
});