forked from UllrAI/SaaS-Starter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnext.config.test.ts
More file actions
128 lines (118 loc) · 3.54 KB
/
next.config.test.ts
File metadata and controls
128 lines (118 loc) · 3.54 KB
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
import {
describe,
it,
expect,
jest,
beforeEach,
afterEach,
} from "@jest/globals";
// Mock next/bundle-analyzer
jest.mock("@next/bundle-analyzer", () => {
const mockWithBundleAnalyzer = jest.fn(
(config: { enabled: boolean }) =>
(nextConfig: Record<string, unknown>) => ({
...nextConfig,
analyzed: true,
}),
);
return mockWithBundleAnalyzer;
});
describe("next.config.ts", () => {
let originalEnv: NodeJS.ProcessEnv;
let consoleErrorSpy: any;
beforeEach(() => {
jest.resetModules(); // Clear module cache before each test
originalEnv = process.env; // Store original process.env
process.env = { ...originalEnv }; // Create a writable copy
consoleErrorSpy = jest.spyOn(console, "error").mockImplementation(() => {});
});
afterEach(() => {
process.env = originalEnv; // Restore original process.env
consoleErrorSpy.mockRestore();
});
it("should enable bundle analyzer when ANALYZE is 'true'", async () => {
process.env.ANALYZE = "true";
// Mock @/env locally for this test
jest.doMock("@/env", () => ({
__esModule: true,
default: {
R2_PUBLIC_URL: "https://test-r2.example.com",
},
}));
const nextConfig = await import("./next.config");
expect(nextConfig).toHaveProperty("analyzed", true);
});
it("should not enable bundle analyzer when ANALYZE is not 'true'", async () => {
process.env.ANALYZE = "false"; // Or any other value
// Mock @/env locally for this test
jest.doMock("@/env", () => ({
__esModule: true,
default: {
R2_PUBLIC_URL: "https://test-r2.example.com",
},
}));
const nextConfig = await import("./next.config");
expect(nextConfig).not.toHaveProperty("analyzed");
});
it("should handle invalid R2_PUBLIC_URL gracefully", async () => {
jest.doMock("@/env", () => ({
__esModule: true,
default: {
R2_PUBLIC_URL: "invalid-url",
},
}));
const nextConfig = await import("./next.config");
expect(consoleErrorSpy).toHaveBeenCalledWith(
"\x1b[33m%s\x1b[0m",
"Warning: Invalid R2_PUBLIC_URL found in environment variables. Skipping R2 remote pattern.",
);
expect((nextConfig as any).images.remotePatterns).not.toContainEqual({
protocol: "https",
hostname: "invalid-url",
});
});
it("should include R2 hostname in remotePatterns if R2_PUBLIC_URL is valid", async () => {
process.env.R2_PUBLIC_URL = "https://valid-r2.example.com";
jest.doMock("@/env", () => ({
__esModule: true,
default: {
R2_PUBLIC_URL: "https://valid-r2.example.com",
},
}));
const nextConfig = await import("./next.config");
expect((nextConfig as any).images.remotePatterns).toEqual([
{
protocol: "https",
hostname: "images.unsplash.com",
},
{
protocol: "https",
hostname: "unsplash.com",
},
{
protocol: "https",
hostname: "valid-r2.example.com",
},
]);
});
it("should not include R2 hostname in remotePatterns if R2_PUBLIC_URL is not set", async () => {
process.env.R2_PUBLIC_URL = undefined;
jest.doMock("@/env", () => ({
__esModule: true,
default: {
R2_PUBLIC_URL: undefined,
},
}));
const nextConfig = await import("./next.config");
expect((nextConfig as any).images.remotePatterns).toEqual([
{
protocol: "https",
hostname: "images.unsplash.com",
},
{
protocol: "https",
hostname: "unsplash.com",
},
]);
});
});