-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathwrappingLoader.test.ts
105 lines (87 loc) · 3.33 KB
/
wrappingLoader.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
import * as fs from 'fs';
import * as path from 'path';
import { describe, vi, it, expect } from 'vitest';
vi.mock('fs', { spy: true });
const originalReadfileSync = fs.readFileSync;
vi.spyOn(fs, 'readFileSync').mockImplementation((filePath, options) => {
if (filePath.toString().endsWith('/config/templates/apiWrapperTemplate.js')) {
return originalReadfileSync(
path.join(__dirname, '../../build/cjs/config/templates/apiWrapperTemplate.js'),
options,
);
}
if (filePath.toString().endsWith('/config/templates/pageWrapperTemplate.js')) {
return originalReadfileSync(
path.join(__dirname, '../../build/cjs/config/templates/pageWrapperTemplate.js'),
options,
);
}
if (filePath.toString().endsWith('/config/templates/middlewareWrapperTemplate.js')) {
return originalReadfileSync(
path.join(__dirname, '../../build/cjs/config/templates/middlewareWrapperTemplate.js'),
options,
);
}
if (filePath.toString().endsWith('/config/templates/sentryInitWrapperTemplate.js')) {
return originalReadfileSync(
path.join(__dirname, '../../build/cjs/config/templates/sentryInitWrapperTemplate.js'),
options,
);
}
if (filePath.toString().endsWith('/config/templates/serverComponentWrapperTemplate.js')) {
return originalReadfileSync(
path.join(__dirname, '../../build/cjs/config/templates/serverComponentWrapperTemplate.js'),
options,
);
}
if (filePath.toString().endsWith('/config/templates/routeHandlerWrapperTemplate.js')) {
return originalReadfileSync(
path.join(__dirname, '../../build/cjs/config/templates/routeHandlerWrapperTemplate.js'),
options,
);
}
return originalReadfileSync(filePath, options);
});
import type { LoaderThis } from '../../src/config/loaders/types';
import type { WrappingLoaderOptions } from '../../src/config/loaders/wrappingLoader';
const { default: wrappingLoader } = await import('../../src/config/loaders/wrappingLoader');
const DEFAULT_PAGE_EXTENSION_REGEX = ['tsx', 'ts', 'jsx', 'js'].join('|');
const defaultLoaderThis = {
addDependency: () => undefined,
async: () => undefined,
cacheable: () => undefined,
};
describe('wrappingLoader', () => {
it('should correctly wrap API routes on unix', async () => {
const callback = vi.fn();
const userCode = `
export default function handler(req, res) {
res.json({ foo: "bar" });
}
`;
const userCodeSourceMap = undefined;
const loaderPromise = new Promise<void>(resolve => {
const loaderThis = {
...defaultLoaderThis,
resourcePath: '/my/pages/my/route.ts',
callback: callback.mockImplementation(() => {
resolve();
}),
getOptions() {
return {
pagesDir: '/my/pages',
appDir: '/my/app',
pageExtensionRegex: DEFAULT_PAGE_EXTENSION_REGEX,
excludeServerRoutes: [],
wrappingTargetKind: 'api-route',
vercelCronsConfig: undefined,
nextjsRequestAsyncStorageModulePath: '/my/request-async-storage.js',
};
},
} satisfies LoaderThis<WrappingLoaderOptions>;
wrappingLoader.call(loaderThis, userCode, userCodeSourceMap);
});
await loaderPromise;
expect(callback).toHaveBeenCalledWith(null, expect.stringContaining("'/my/route'"), expect.anything());
});
});