|
1 | | -import { beforeEach, describe, expect, it, vi } from "vitest"; |
2 | | - |
3 | | -import type { NextConfigComplete } from "next/dist/server/config-shared.js"; |
4 | | - |
5 | | -vi.mock("next/dist/build/load-jsconfig.js", () => ({ |
6 | | - default: vi.fn(), |
7 | | -})); |
8 | | - |
9 | | -vi.mock("next/dist/build/swc/index.js", () => ({ |
10 | | - transform: vi.fn(), |
11 | | -})); |
12 | | - |
13 | | -vi.mock("../../utils/nextjs", () => ({ |
14 | | - findNextDirectories: vi.fn(), |
15 | | - loadClosestPackageJson: vi.fn(), |
16 | | - loadSWCBindingsEagerly: vi.fn(), |
17 | | -})); |
18 | | - |
19 | | -vi.mock("../../utils/swc/transform", () => ({ |
20 | | - getVitestSWCTransformConfig: vi.fn(), |
21 | | -})); |
22 | | - |
23 | | -const createPromiseWithResolvers = <T,>() => { |
24 | | - let resolve!: (value: T | PromiseLike<T>) => void; |
25 | | - let reject!: (reason?: unknown) => void; |
26 | | - const promise = new Promise<T>((res, rej) => { |
27 | | - resolve = res; |
28 | | - reject = rej; |
29 | | - }); |
30 | | - |
31 | | - return { promise, resolve, reject }; |
32 | | -}; |
33 | | - |
34 | | -const nextConfig: NextConfigComplete = { |
35 | | - // Only the fields read by our code under test are populated here. |
36 | | - experimental: { |
37 | | - swcPlugins: [], |
38 | | - }, |
39 | | - modularizeImports: undefined, |
40 | | - compiler: undefined, |
41 | | - distDir: ".next", |
42 | | - // biome-ignore lint/suspicious/noExplicitAny: we only need a partial shape for this test |
43 | | -} as any; |
44 | | - |
45 | | -describe("vitePluginNextSwc env detection", () => { |
46 | | - const setupMocks = async () => { |
47 | | - const loadJsConfigModule = await import("next/dist/build/load-jsconfig.js"); |
48 | | - vi.mocked(loadJsConfigModule.default).mockResolvedValue( |
49 | | - // biome-ignore lint/suspicious/noExplicitAny: Next's load-jsconfig return type is not stable/public |
50 | | - { |
51 | | - useTypeScript: true, |
52 | | - jsConfig: { compilerOptions: {} }, |
53 | | - resolvedBaseUrl: undefined, |
54 | | - } as any, |
55 | | - ); |
56 | | - |
57 | | - const NextUtils = await import("../../utils/nextjs"); |
58 | | - vi.mocked(NextUtils.findNextDirectories).mockReturnValue({ |
59 | | - pagesDir: "/pages", |
60 | | - appDir: "/app", |
61 | | - }); |
62 | | - vi.mocked(NextUtils.loadClosestPackageJson).mockResolvedValue({ |
63 | | - type: "module", |
64 | | - }); |
65 | | - vi.mocked(NextUtils.loadSWCBindingsEagerly).mockResolvedValue(undefined); |
66 | | - |
67 | | - const swc = await import("next/dist/build/swc/index.js"); |
68 | | - vi.mocked(swc.transform).mockResolvedValue({ |
69 | | - code: "export {}", |
70 | | - map: null, |
71 | | - }); |
72 | | - |
73 | | - const swcTransform = await import("../../utils/swc/transform"); |
74 | | - vi.mocked(swcTransform.getVitestSWCTransformConfig).mockReturnValue( |
75 | | - // biome-ignore lint/suspicious/noExplicitAny: Next's SWC options are not exported as a stable TS type |
76 | | - {} as any, |
77 | | - ); |
78 | | - }; |
79 | | - |
80 | | - it("treats Storybook-like Vite config (no `test` field) as browser, not server", async () => { |
81 | | - // In Storybook, VITEST is not set; in our unit test runner it is, so we explicitly simulate Storybook. |
82 | | - process.env.VITEST = "false"; |
83 | | - vi.resetModules(); |
84 | | - await setupMocks(); |
85 | | - |
86 | | - const { vitePluginNextSwc } = await import("./plugin"); |
87 | | - |
88 | | - const nextConfigResolver = createPromiseWithResolvers<NextConfigComplete>(); |
89 | | - nextConfigResolver.resolve(nextConfig); |
90 | | - |
91 | | - const plugin = vitePluginNextSwc("/root", nextConfigResolver); |
92 | | - |
93 | | - await plugin.config?.({}, { mode: "development" } as never); |
94 | | - |
95 | | - await plugin.transform?.call( |
96 | | - { getCombinedSourcemap: () => ({ version: 3, mappings: "" } as any) } as any, |
97 | | - "export const x = typeof window;", |
98 | | - "/src/example.ts", |
99 | | - ); |
100 | | - |
101 | | - const swcTransform = await import("../../utils/swc/transform"); |
102 | | - const lastCallArg = |
103 | | - vi.mocked(swcTransform.getVitestSWCTransformConfig).mock.calls.at(-1)?.[0]; |
104 | | - |
105 | | - expect(lastCallArg?.isServerEnvironment).toBe(false); |
106 | | - }); |
107 | | - |
108 | | - it("treats Vitest node environment as server", async () => { |
109 | | - process.env.VITEST = "true"; |
110 | | - vi.resetModules(); |
111 | | - await setupMocks(); |
112 | | - |
113 | | - const { vitePluginNextSwc } = await import("./plugin"); |
114 | | - |
115 | | - const nextConfigResolver = createPromiseWithResolvers<NextConfigComplete>(); |
116 | | - nextConfigResolver.resolve(nextConfig); |
117 | | - |
118 | | - const plugin = vitePluginNextSwc("/root", nextConfigResolver); |
119 | | - |
120 | | - await plugin.config?.( |
121 | | - { test: { environment: "node" } }, |
122 | | - { mode: "development" } as never, |
123 | | - ); |
124 | | - |
125 | | - await plugin.transform?.call( |
126 | | - { getCombinedSourcemap: () => ({ version: 3, mappings: "" } as any) } as any, |
127 | | - "export const x = typeof window;", |
128 | | - "/src/example.ts", |
129 | | - ); |
130 | | - |
131 | | - const swcTransform = await import("../../utils/swc/transform"); |
132 | | - const lastCallArg = |
133 | | - vi.mocked(swcTransform.getVitestSWCTransformConfig).mock.calls.at(-1)?.[0]; |
134 | | - |
135 | | - expect(lastCallArg?.isServerEnvironment).toBe(true); |
136 | | - }); |
137 | | -}); |
138 | | - |
139 | | -
|
| 1 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | + |
| 3 | +import type loadJsConfig from "next/dist/build/load-jsconfig.js"; |
| 4 | +import type { NextConfigComplete } from "next/dist/server/config-shared.js"; |
| 5 | + |
| 6 | +vi.mock("next/dist/build/load-jsconfig.js", () => ({ |
| 7 | + default: vi.fn(), |
| 8 | +})); |
| 9 | + |
| 10 | +vi.mock("next/dist/build/swc/index.js", () => ({ |
| 11 | + transform: vi.fn(), |
| 12 | +})); |
| 13 | + |
| 14 | +vi.mock("../../utils/nextjs", () => ({ |
| 15 | + findNextDirectories: vi.fn(), |
| 16 | + loadClosestPackageJson: vi.fn(), |
| 17 | + loadSWCBindingsEagerly: vi.fn(), |
| 18 | +})); |
| 19 | + |
| 20 | +vi.mock("../../utils/swc/transform", () => ({ |
| 21 | + getVitestSWCTransformConfig: vi.fn(), |
| 22 | +})); |
| 23 | + |
| 24 | +const createPromiseWithResolvers = <T>() => { |
| 25 | + let resolve!: (value: T | PromiseLike<T>) => void; |
| 26 | + let reject!: (reason?: unknown) => void; |
| 27 | + const promise = new Promise<T>((res, rej) => { |
| 28 | + resolve = res; |
| 29 | + reject = rej; |
| 30 | + }); |
| 31 | + |
| 32 | + return { promise, resolve, reject }; |
| 33 | +}; |
| 34 | + |
| 35 | +const nextConfig: NextConfigComplete = { |
| 36 | + // Only the fields read by our code under test are populated here. |
| 37 | + experimental: { |
| 38 | + swcPlugins: [], |
| 39 | + }, |
| 40 | + modularizeImports: undefined, |
| 41 | + compiler: undefined, |
| 42 | + distDir: ".next", |
| 43 | + // biome-ignore lint/suspicious/noExplicitAny: we only need a partial shape for this test |
| 44 | +} as any; |
| 45 | + |
| 46 | +describe("vitePluginNextSwc env detection", () => { |
| 47 | + const setupMocks = async () => { |
| 48 | + const loadJsConfigModule = await import("next/dist/build/load-jsconfig.js"); |
| 49 | + vi.mocked(loadJsConfigModule.default).mockResolvedValue({ |
| 50 | + useTypeScript: true, |
| 51 | + jsConfig: { compilerOptions: {} }, |
| 52 | + resolvedBaseUrl: undefined, |
| 53 | + } as unknown as Awaited<ReturnType<typeof loadJsConfig>>); |
| 54 | + |
| 55 | + const NextUtils = await import("../../utils/nextjs"); |
| 56 | + vi.mocked(NextUtils.findNextDirectories).mockReturnValue({ |
| 57 | + pagesDir: "/pages", |
| 58 | + appDir: "/app", |
| 59 | + }); |
| 60 | + vi.mocked(NextUtils.loadClosestPackageJson).mockResolvedValue({ |
| 61 | + type: "module", |
| 62 | + }); |
| 63 | + vi.mocked(NextUtils.loadSWCBindingsEagerly).mockResolvedValue(undefined); |
| 64 | + |
| 65 | + const swc = await import("next/dist/build/swc/index.js"); |
| 66 | + vi.mocked(swc.transform).mockResolvedValue({ |
| 67 | + code: "export {}", |
| 68 | + map: null, |
| 69 | + }); |
| 70 | + |
| 71 | + const swcTransform = await import("../../utils/swc/transform"); |
| 72 | + vi.mocked(swcTransform.getVitestSWCTransformConfig).mockReturnValue( |
| 73 | + {} as ReturnType<typeof swcTransform.getVitestSWCTransformConfig>, |
| 74 | + ); |
| 75 | + }; |
| 76 | + |
| 77 | + it("treats Storybook-like Vite config (no `test` field) as browser, not server", async () => { |
| 78 | + // In Storybook, VITEST is not set; in our unit test runner it is, so we explicitly simulate Storybook. |
| 79 | + process.env.VITEST = "false"; |
| 80 | + vi.resetModules(); |
| 81 | + await setupMocks(); |
| 82 | + |
| 83 | + const { vitePluginNextSwc } = await import("./plugin"); |
| 84 | + |
| 85 | + const nextConfigResolver = createPromiseWithResolvers<NextConfigComplete>(); |
| 86 | + nextConfigResolver.resolve(nextConfig); |
| 87 | + |
| 88 | + const plugin = vitePluginNextSwc("/root", nextConfigResolver); |
| 89 | + |
| 90 | + await plugin.config?.({}, { mode: "development" } as never); |
| 91 | + |
| 92 | + await plugin.transform?.call( |
| 93 | + { getCombinedSourcemap: () => null } as unknown as ThisParameterType< |
| 94 | + NonNullable<typeof plugin.transform> |
| 95 | + >, |
| 96 | + "export const x = typeof window;", |
| 97 | + "/src/example.ts", |
| 98 | + ); |
| 99 | + |
| 100 | + const swcTransform = await import("../../utils/swc/transform"); |
| 101 | + const lastCallArg = vi |
| 102 | + .mocked(swcTransform.getVitestSWCTransformConfig) |
| 103 | + .mock.calls.at(-1)?.[0]; |
| 104 | + |
| 105 | + expect(lastCallArg?.isServerEnvironment).toBe(false); |
| 106 | + }); |
| 107 | + |
| 108 | + it("treats Vitest node environment as server", async () => { |
| 109 | + process.env.VITEST = "true"; |
| 110 | + vi.resetModules(); |
| 111 | + await setupMocks(); |
| 112 | + |
| 113 | + const { vitePluginNextSwc } = await import("./plugin"); |
| 114 | + |
| 115 | + const nextConfigResolver = createPromiseWithResolvers<NextConfigComplete>(); |
| 116 | + nextConfigResolver.resolve(nextConfig); |
| 117 | + |
| 118 | + const plugin = vitePluginNextSwc("/root", nextConfigResolver); |
| 119 | + |
| 120 | + await plugin.config?.({ test: { environment: "node" } }, { |
| 121 | + mode: "development", |
| 122 | + } as never); |
| 123 | + |
| 124 | + await plugin.transform?.call( |
| 125 | + { getCombinedSourcemap: () => null } as unknown as ThisParameterType< |
| 126 | + NonNullable<typeof plugin.transform> |
| 127 | + >, |
| 128 | + "export const x = typeof window;", |
| 129 | + "/src/example.ts", |
| 130 | + ); |
| 131 | + |
| 132 | + const swcTransform = await import("../../utils/swc/transform"); |
| 133 | + const lastCallArg = vi |
| 134 | + .mocked(swcTransform.getVitestSWCTransformConfig) |
| 135 | + .mock.calls.at(-1)?.[0]; |
| 136 | + |
| 137 | + expect(lastCallArg?.isServerEnvironment).toBe(true); |
| 138 | + }); |
| 139 | +}); |
0 commit comments