Skip to content

Commit 7073377

Browse files
committed
chore: re-enable custom server tests and add react tests
1 parent 9b415b5 commit 7073377

File tree

1 file changed

+107
-8
lines changed

1 file changed

+107
-8
lines changed

test/templates/server.spec.ts

+107-8
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,54 @@ jest.mock(
6565
{ virtual: true },
6666
)
6767

68+
jest.mock(
69+
'server/app-paths-manifest.json',
70+
() => ({
71+
'/blog/(test)/[author]/[slug]/page': 'app/blog/[author]/[slug]/page.js',
72+
}),
73+
{ virtual: true },
74+
)
75+
76+
jest.mock(
77+
'routes-manifest.json',
78+
() => ({
79+
dynamicRoutes: [
80+
{
81+
page: '/posts/[title]',
82+
regex: '^/posts/([^/]+?)(?:/)?$',
83+
routeKeys: {
84+
nxtPtitle: 'nxtPtitle',
85+
},
86+
namedRegex: '^/posts/(?<nxtPtitle>[^/]+?)(?:/)?$',
87+
},
88+
{
89+
page: '/blog/[author]/[slug]',
90+
regex: '^/blog/([^/]+?)/([^/]+?)(?:/)?$',
91+
routeKeys: {
92+
nxtPauthor: 'nxtPauthor',
93+
nxtPslug: 'nxtPslug',
94+
},
95+
namedRegex: '^/blog/(?<nxtPauthor>[^/]+?)/(?<nxtPslug>[^/]+?)(?:/)?$',
96+
},
97+
],
98+
staticRoutes: [
99+
{
100+
page: '/non-i18n/with-revalidate',
101+
regex: '^/non-i18n/with-revalidate(?:/)?$',
102+
routeKeys: {},
103+
namedRegex: '^/non-i18n/with-revalidate(?:/)?$',
104+
},
105+
{
106+
page: '/i18n/with-revalidate',
107+
regex: '^/i18n/with-revalidate(?:/)?$',
108+
routeKeys: {},
109+
namedRegex: '^/i18n/with-revalidate(?:/)?$',
110+
},
111+
],
112+
}),
113+
{ virtual: true },
114+
)
115+
68116
let NetlifyNextServer: NetlifyNextServerType
69117
beforeAll(() => {
70118
const NextServer: NextServerType = require(getServerFile(__dirname, false)).default
@@ -76,12 +124,15 @@ beforeAll(() => {
76124
this.buildId = mockBuildId
77125
this.nextConfig = nextOptions.conf
78126
this.netlifyConfig = netlifyConfig
127+
this.renderOpts = { previewProps: {} }
128+
this.hasAppDir = Boolean(this.nextConfig?.experimental?.appDir)
129+
this.appPathsManifest = this.getAppPathsManifest()
79130
}
80131
Object.setPrototypeOf(NetlifyNextServer, MockNetlifyNextServerConstructor)
81132
})
82133

83134
describe('the netlify next server', () => {
84-
it.skip('does not revalidate a request without an `x-prerender-revalidate` header', async () => {
135+
it('does not revalidate a request without an `x-prerender-revalidate` header', async () => {
85136
const netlifyNextServer = new NetlifyNextServer({ conf: {} }, { ...mockTokenConfig })
86137
const requestHandler = netlifyNextServer.getRequestHandler()
87138

@@ -92,7 +143,7 @@ describe('the netlify next server', () => {
92143
expect(mockedApiFetch).not.toHaveBeenCalled()
93144
})
94145

95-
it.skip('revalidates a static non-i18n route with an `x-prerender-revalidate` header', async () => {
146+
it('revalidates a static non-i18n route with an `x-prerender-revalidate` header', async () => {
96147
const netlifyNextServer = new NetlifyNextServer({ conf: {} }, { ...mockTokenConfig })
97148
const requestHandler = netlifyNextServer.getRequestHandler()
98149

@@ -112,7 +163,7 @@ describe('the netlify next server', () => {
112163
)
113164
})
114165

115-
it.skip('revalidates a static i18n route with an `x-prerender-revalidate` header', async () => {
166+
it('revalidates a static i18n route with an `x-prerender-revalidate` header', async () => {
116167
const netlifyNextServer = new NetlifyNextServer({ conf: { ...mocki18nConfig } }, { ...mockTokenConfig })
117168
const requestHandler = netlifyNextServer.getRequestHandler()
118169

@@ -132,7 +183,7 @@ describe('the netlify next server', () => {
132183
)
133184
})
134185

135-
it.skip('revalidates a dynamic non-i18n route with an `x-prerender-revalidate` header', async () => {
186+
it('revalidates a dynamic non-i18n route with an `x-prerender-revalidate` header', async () => {
136187
const netlifyNextServer = new NetlifyNextServer({ conf: {} }, { ...mockTokenConfig })
137188
const requestHandler = netlifyNextServer.getRequestHandler()
138189

@@ -152,7 +203,7 @@ describe('the netlify next server', () => {
152203
)
153204
})
154205

155-
it.skip('revalidates a dynamic i18n route with an `x-prerender-revalidate` header', async () => {
206+
it('revalidates a dynamic i18n route with an `x-prerender-revalidate` header', async () => {
156207
const netlifyNextServer = new NetlifyNextServer({ conf: { ...mocki18nConfig } }, { ...mockTokenConfig })
157208
const requestHandler = netlifyNextServer.getRequestHandler()
158209

@@ -172,7 +223,7 @@ describe('the netlify next server', () => {
172223
)
173224
})
174225

175-
it.skip('throws an error when route is not found in the manifest', async () => {
226+
it('throws an error when route is not found in the manifest', async () => {
176227
const netlifyNextServer = new NetlifyNextServer({ conf: {} }, mockTokenConfig)
177228
const requestHandler = netlifyNextServer.getRequestHandler()
178229

@@ -187,7 +238,7 @@ describe('the netlify next server', () => {
187238
)
188239
})
189240

190-
it.skip('throws an error when paths are not found by the API', async () => {
241+
it('throws an error when paths are not found by the API', async () => {
191242
const netlifyNextServer = new NetlifyNextServer({ conf: {} }, mockTokenConfig)
192243
const requestHandler = netlifyNextServer.getRequestHandler()
193244

@@ -203,7 +254,7 @@ describe('the netlify next server', () => {
203254
)
204255
})
205256

206-
it.skip('throws an error when the revalidate API is unreachable', async () => {
257+
it('throws an error when the revalidate API is unreachable', async () => {
207258
const netlifyNextServer = new NetlifyNextServer({ conf: {} }, mockTokenConfig)
208259
const requestHandler = netlifyNextServer.getRequestHandler()
209260

@@ -218,4 +269,52 @@ describe('the netlify next server', () => {
218269
'Unable to connect',
219270
)
220271
})
272+
273+
it('resolves react as normal for pages routes', async () => {
274+
const netlifyNextServer = new NetlifyNextServer({ conf: {} }, {})
275+
const requestHandler = netlifyNextServer.getRequestHandler()
276+
277+
const { req: mockReq, res: mockRes } = createRequestResponseMocks({
278+
url: '/posts/hello',
279+
})
280+
281+
// @ts-expect-error - Types are incorrect for `MockedResponse`
282+
await requestHandler(new NodeNextRequest(mockReq), new NodeNextResponse(mockRes))
283+
284+
// eslint-disable-next-line no-underscore-dangle
285+
expect(process.env.__NEXT_PRIVATE_PREBUNDLED_REACT).toBe('')
286+
})
287+
288+
it('resolves the prebundled react version for app routes', async () => {
289+
const netlifyNextServer = new NetlifyNextServer({ conf: { experimental: { appDir: true } } }, {})
290+
const requestHandler = netlifyNextServer.getRequestHandler()
291+
292+
const { req: mockReq, res: mockRes } = createRequestResponseMocks({
293+
url: '/blog/rob/hello',
294+
})
295+
296+
// @ts-expect-error - Types are incorrect for `MockedResponse`
297+
await requestHandler(new NodeNextRequest(mockReq), new NodeNextResponse(mockRes))
298+
299+
// eslint-disable-next-line no-underscore-dangle
300+
expect(process.env.__NEXT_PRIVATE_PREBUNDLED_REACT).toBe('next')
301+
})
302+
303+
it('resolves the experimental prebundled react version for app routes with server actions', async () => {
304+
const netlifyNextServer = new NetlifyNextServer(
305+
{ conf: { experimental: { appDir: true, serverActions: true } } },
306+
{},
307+
)
308+
const requestHandler = netlifyNextServer.getRequestHandler()
309+
310+
const { req: mockReq, res: mockRes } = createRequestResponseMocks({
311+
url: '/blog/rob/hello',
312+
})
313+
314+
// @ts-expect-error - Types are incorrect for `MockedResponse`
315+
await requestHandler(new NodeNextRequest(mockReq), new NodeNextResponse(mockRes))
316+
317+
// eslint-disable-next-line no-underscore-dangle
318+
expect(process.env.__NEXT_PRIVATE_PREBUNDLED_REACT).toBe('experimental')
319+
})
221320
})

0 commit comments

Comments
 (0)