-
Notifications
You must be signed in to change notification settings - Fork 326
/
Copy pathnext-build.test.ts
210 lines (182 loc) · 5.63 KB
/
next-build.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import { expect, test } from '@playwright/test';
import type { Application } from '../models/application';
import { appConfigs } from '../presets';
type RenderingModeTestCase = {
name: string;
type: 'Static' | 'Dynamic';
page: string;
};
function getIndicator(buildOutput: string, type: 'Static' | 'Dynamic') {
return buildOutput
.split('\n')
.find(msg => {
const isTypeFound = msg.includes(`(${type})`);
if (type === 'Dynamic') {
return isTypeFound || msg.includes(`(Server)`);
}
return isTypeFound;
})
.split(' ')[0];
}
test.describe('next build - provider as client component @nextjs', () => {
test.describe.configure({ mode: 'parallel' });
let app: Application;
test.beforeAll(async () => {
app = await appConfigs.next.appRouter
.clone()
.addFile(
'src/app/provider.tsx',
() => `'use client'
import { ClerkProvider } from "@clerk/nextjs"
export function Provider({ children }: { children: any }) {
return (
<ClerkProvider>
{children}
</ClerkProvider>
)
}`,
)
.addFile(
'src/app/layout.tsx',
() => `import './globals.css';
import { Inter } from 'next/font/google';
import { Provider } from './provider';
const inter = Inter({ subsets: ['latin'] });
export const metadata = {
title: 'Create Next App',
description: 'Generated by create next app',
};
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<Provider>
<html lang='en'>
<body className={inter.className}>{children}</body>
</html>
</Provider>
);
}
`,
)
.commit();
await app.setup();
await app.withEnv(appConfigs.envs.withEmailCodes);
await app.build();
});
test.afterAll(async () => {
await app.teardown();
});
test('When <ClerkProvider /> is used as a client component, builds successfully and does not force dynamic rendering', () => {
// Get the static indicator from the build output
const staticIndicator = getIndicator(app.buildOutput, 'Static');
/**
* Using /_not-found as it is an internal page that should statically render by default.
* This is a good indicator of whether or not the entire app has been opted-in to dynamic rendering.
*/
const notFoundPageLine = app.buildOutput.split('\n').find(msg => msg.includes('/_not-found'));
expect(notFoundPageLine).toContain(staticIndicator);
});
/**
* Sometimes utilities from `/server` may use Node APIs even if `clerkMiddleware` does not consumes them.
* This happends because of code for node runtime and edge runtime is bundled together in the `/server/index.ts` barrel file.
* This test ensures that developers will not end up with warnings on `next build`.
*/
test('Avoid import traces logs indicating misuse of node apis inside middleware', () => {
expect(app.buildOutput).not.toMatch(/import trace/i);
});
});
test.describe('next build - dynamic options @nextjs', () => {
test.describe.configure({ mode: 'parallel' });
let app: Application;
test.beforeAll(async () => {
app = await appConfigs.next.appRouter
.clone()
.addFile(
'src/app/(dynamic)/layout.tsx',
() => `import '../globals.css';
import { Inter } from 'next/font/google';
import { ClerkProvider } from '@clerk/nextjs';
const inter = Inter({ subsets: ['latin'] });
export const metadata = {
title: 'Create Next App',
description: 'Generated by create next app',
};
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<ClerkProvider dynamic>
<html lang='en'>
<body className={inter.className}>{children}</body>
</html>
</ClerkProvider>
);
}
`,
)
.addFile(
'src/app/(dynamic)/dynamic/page.tsx',
() => `export default function DynamicPage() {
return(<h1>This page is dynamic</h1>);
}`,
)
.addFile(
'src/app/nested-provider/page.tsx',
() => `import { ClerkProvider } from '@clerk/nextjs';
import { ClientComponent } from './client';
export default function Page() {
return (
<ClerkProvider dynamic>
<ClientComponent />
</ClerkProvider>
);
}
`,
)
.addFile(
'src/app/nested-provider/client.tsx',
() => `'use client';
import { useAuth } from '@clerk/nextjs';
export function ClientComponent() {
useAuth();
return <p>I am dynamically rendered</p>;
}
`,
)
.commit();
await app.setup();
await app.withEnv(appConfigs.envs.withEmailCodes);
await app.build();
});
test.afterAll(async () => {
// await app.teardown();
});
(
[
{
name: '<ClerkProvider> supports static rendering by default',
type: 'Static',
page: '/_not-found',
},
{
name: '<ClerkProvider dynamic> opts-in to dynamic rendering',
type: 'Dynamic',
page: '/dynamic',
},
{
name: 'auth() opts in to dynamic rendering',
type: 'Dynamic',
page: '/page-protected',
},
{
name: '<ClerkProvider dynamic> can be nested in the root provider',
type: 'Dynamic',
page: '/nested-provider',
},
] satisfies RenderingModeTestCase[]
).forEach(({ name, type, page }) => {
test(`ClerkProvider rendering modes - ${name}`, () => {
// Get the indicator from the build output
const indicator = getIndicator(app.buildOutput, type);
const pageLine = app.buildOutput.split('\n').find(msg => msg.includes(` ${page}`));
expect(pageLine).toContain(indicator);
});
});
});