|
| 1 | +import * as fs from 'fs'; |
| 2 | +import * as path from 'path'; |
| 3 | + |
| 4 | +const originalReadfileSync = fs.readFileSync; |
| 5 | + |
| 6 | +jest.spyOn(fs, 'readFileSync').mockImplementation((filePath, options) => { |
| 7 | + if (filePath.toString().endsWith('/config/templates/apiWrapperTemplate.js')) { |
| 8 | + return originalReadfileSync( |
| 9 | + path.join(__dirname, '../../build/cjs/config/templates/apiWrapperTemplate.js'), |
| 10 | + options, |
| 11 | + ); |
| 12 | + } |
| 13 | + |
| 14 | + if (filePath.toString().endsWith('/config/templates/pageWrapperTemplate.js')) { |
| 15 | + return originalReadfileSync( |
| 16 | + path.join(__dirname, '../../build/cjs/config/templates/pageWrapperTemplate.js'), |
| 17 | + options, |
| 18 | + ); |
| 19 | + } |
| 20 | + |
| 21 | + if (filePath.toString().endsWith('/config/templates/middlewareWrapperTemplate.js')) { |
| 22 | + return originalReadfileSync( |
| 23 | + path.join(__dirname, '../../build/cjs/config/templates/middlewareWrapperTemplate.js'), |
| 24 | + options, |
| 25 | + ); |
| 26 | + } |
| 27 | + |
| 28 | + if (filePath.toString().endsWith('/config/templates/sentryInitWrapperTemplate.js')) { |
| 29 | + return originalReadfileSync( |
| 30 | + path.join(__dirname, '../../build/cjs/config/templates/sentryInitWrapperTemplate.js'), |
| 31 | + options, |
| 32 | + ); |
| 33 | + } |
| 34 | + |
| 35 | + if (filePath.toString().endsWith('/config/templates/serverComponentWrapperTemplate.js')) { |
| 36 | + return originalReadfileSync( |
| 37 | + path.join(__dirname, '../../build/cjs/config/templates/serverComponentWrapperTemplate.js'), |
| 38 | + options, |
| 39 | + ); |
| 40 | + } |
| 41 | + |
| 42 | + if (filePath.toString().endsWith('/config/templates/routeHandlerWrapperTemplate.js')) { |
| 43 | + return originalReadfileSync( |
| 44 | + path.join(__dirname, '../../build/cjs/config/templates/routeHandlerWrapperTemplate.js'), |
| 45 | + options, |
| 46 | + ); |
| 47 | + } |
| 48 | + |
| 49 | + return originalReadfileSync(filePath, options); |
| 50 | +}); |
| 51 | + |
| 52 | +import type { LoaderThis } from '../../src/config/loaders/types'; |
| 53 | +import type { WrappingLoaderOptions } from '../../src/config/loaders/wrappingLoader'; |
| 54 | +import wrappingLoader from '../../src/config/loaders/wrappingLoader'; |
| 55 | + |
| 56 | +const DEFAULT_PAGE_EXTENSION_REGEX = ['tsx', 'ts', 'jsx', 'js'].join('|'); |
| 57 | + |
| 58 | +const defaultLoaderThis = { |
| 59 | + addDependency: () => undefined, |
| 60 | + async: () => undefined, |
| 61 | + cacheable: () => undefined, |
| 62 | +}; |
| 63 | + |
| 64 | +describe('wrappingLoader', () => { |
| 65 | + it('should correctly wrap API routes on unix', async () => { |
| 66 | + const callback = jest.fn(); |
| 67 | + |
| 68 | + const userCode = ` |
| 69 | + export default function handler(req, res) { |
| 70 | + res.json({ foo: "bar" }); |
| 71 | + } |
| 72 | + `; |
| 73 | + const userCodeSourceMap = undefined; |
| 74 | + |
| 75 | + const loaderPromise = new Promise<void>(resolve => { |
| 76 | + const loaderThis = { |
| 77 | + ...defaultLoaderThis, |
| 78 | + resourcePath: '/my/pages/my/route.ts', |
| 79 | + callback: callback.mockImplementation(() => { |
| 80 | + resolve(); |
| 81 | + }), |
| 82 | + getOptions() { |
| 83 | + return { |
| 84 | + pagesDir: '/my/pages', |
| 85 | + appDir: '/my/app', |
| 86 | + pageExtensionRegex: DEFAULT_PAGE_EXTENSION_REGEX, |
| 87 | + excludeServerRoutes: [], |
| 88 | + wrappingTargetKind: 'api-route', |
| 89 | + sentryConfigFilePath: '/my/sentry.server.config.ts', |
| 90 | + vercelCronsConfig: undefined, |
| 91 | + nextjsRequestAsyncStorageModulePath: '/my/request-async-storage.js', |
| 92 | + }; |
| 93 | + }, |
| 94 | + } satisfies LoaderThis<WrappingLoaderOptions>; |
| 95 | + |
| 96 | + wrappingLoader.call(loaderThis, userCode, userCodeSourceMap); |
| 97 | + }); |
| 98 | + |
| 99 | + await loaderPromise; |
| 100 | + |
| 101 | + expect(callback).toHaveBeenCalledWith(null, expect.stringContaining("'/my/route'"), expect.anything()); |
| 102 | + }); |
| 103 | + |
| 104 | + it.todo('should correctly wrap API routes on unix'); |
| 105 | +}); |
0 commit comments