Skip to content

Commit

Permalink
test: add tests for legacy NextCustomServer methods
Browse files Browse the repository at this point in the history
  • Loading branch information
lubieowoce committed Nov 21, 2024
1 parent 80bbc75 commit ba31fba
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 4 deletions.
1 change: 1 addition & 0 deletions test/integration/custom-server/pages/404.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default () => <p>made it to 404</p>
1 change: 1 addition & 0 deletions test/integration/custom-server/pages/500.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default () => <p>made it to 500</p>
22 changes: 22 additions & 0 deletions test/integration/custom-server/pages/dynamic-dashboard/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { useRouter } from 'next/router'

// NOTE: we want this page to be dynamic, otherwise the HTML won't contain search params
export async function getServerSideProps() {
return { props: {} }
}

export default function Page() {
const router = useRouter()
const searchParam = router.query.q
return (
<p>
made it to dynamic dashboard
{!!searchParam && (
<>
<br />
query param: {searchParam}
</>
)}
</p>
)
}
69 changes: 67 additions & 2 deletions test/integration/custom-server/server.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
// @ts-check

if (process.env.POLYFILL_FETCH) {
// @ts-expect-error
global.fetch = require('node-fetch').default
// @ts-expect-error
global.Request = require('node-fetch').Request
// @ts-expect-error
global.Headers = require('node-fetch').Headers
}

const { readFileSync } = require('fs')

/** @type {import('next').default} */
// @ts-ignore: missing interopDefault
const next = require('next')

const { join } = require('path')
const { parse } = require('url')

const dev = process.env.NODE_ENV !== 'production'
const dir = __dirname
const port = process.env.PORT || 3000
const port =
(process.env.PORT ? Number.parseInt(process.env.PORT) : undefined) || 3000
const { createServer } = require(
process.env.USE_HTTPS === 'true' ? 'https' : 'http'
)
Expand All @@ -30,6 +40,11 @@ process.on('unhandledRejection', (err) => {

app.prepare().then(() => {
const server = createServer(httpOptions, async (req, res) => {
// let next.js handle assets from /_next/
if (/\/_next\//.test(req.url)) {
return handleNextRequests(req, res)
}

if (req.url === '/no-query') {
return app.render(req, res, '/no-query')
}
Expand All @@ -42,7 +57,7 @@ app.prepare().then(() => {
if (/setAssetPrefix/.test(req.url)) {
app.setAssetPrefix(`http://127.0.0.1:${port}`)
} else if (/setEmptyAssetPrefix/.test(req.url)) {
app.setAssetPrefix(null)
app.setAssetPrefix('')
} else {
// This is to support multi-zones support in localhost
// and may be in staging deployments
Expand Down Expand Up @@ -73,6 +88,56 @@ app.prepare().then(() => {
return handleNextRequests(req, res, parse('/dashboard', true))
}

if (/legacy-methods\/render-to-html/.test(req.url)) {
try {
const html = await app.renderToHTML(req, res, '/dynamic-dashboard', {
q: '1',
})
res.end(html)
} catch (err) {
res.end(err.message)
}
return
}

if (/legacy-methods\/render404/.test(req.url)) {
try {
await app.render404(req, res, parse('/__non_existent__?q=1', true))
} catch (err) {
res.end(err.message)
}
return
}

if (/legacy-methods\/render-error/.test(req.url)) {
try {
res.statusCode = 500
await app.renderError(new Error('kaboom'), req, res, '/dashboard', {
q: '1',
})
} catch (err) {
res.end(err.message)
}
return
}

if (/legacy-methods\/render-error-to-html/.test(req.url)) {
try {
res.statusCode = 500
const html = await app.renderErrorToHTML(
new Error('kaboom'),
req,
res,
'/dashboard',
{ q: '1' }
)
res.end(html)
} catch (err) {
res.end(err.message)
}
return
}

handleNextRequests(req, res)
})

Expand Down
70 changes: 69 additions & 1 deletion test/integration/custom-server/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ describe.each([
await fetchViaHTTP(nextUrl, '/unhandled-rejection', undefined, { agent })
await check(() => stderr, /unhandledRejection/)
expect(stderr).toContain('unhandledRejection: Error: unhandled rejection')
expect(stderr).toContain('server.js:38:22')
expect(stderr).toMatch(/\/server\.js:\d+\d+/)
})
})

Expand All @@ -317,4 +317,72 @@ describe.each([
)
})
})

describe.each(['development', 'production'])(
'legacy NextCustomServer methods - %s mode',
(mode) => {
const isNextDev = mode === 'development'

beforeAll(async () => {
if (!isNextDev) {
await nextBuild(appDir)
}
await startServer({ NODE_ENV: mode })
})
afterAll(() => killApp(server))

it('NextCustomServer.renderToHTML', async () => {
const rawHTML = await renderViaHTTP(
nextUrl,
'/legacy-methods/render-to-html?q=2',
undefined,
{ agent }
)
const $ = cheerio.load(rawHTML)
const text = $('p').text()
expect(text).toContain('made it to dynamic dashboard')
expect(text).toContain('query param: 1')
})

it('NextCustomServer.render404', async () => {
const html = await renderViaHTTP(
nextUrl,
'/legacy-methods/render404',
undefined,
{ agent }
)
expect(html).toContain('made it to 404')
})

it('NextCustomServer.renderError', async () => {
const html = await renderViaHTTP(
nextUrl,
'/legacy-methods/render-error',
undefined,
{ agent }
)
if (isNextDev) {
// in dev, we always render error overlay + default error page, not /500
expect(html).toContain('Error: kaboom')
} else {
expect(html).toContain('made it to 500')
}
})

it('NextCustomServer.renderErrorToHTML', async () => {
const html = await renderViaHTTP(
nextUrl,
'/legacy-methods/render-error-to-html',
undefined,
{ agent }
)
if (isNextDev) {
// in dev, we always render error overlay + default error page, not /500
expect(html).toContain('Error: kaboom')
} else {
expect(html).toContain('made it to 500')
}
})
}
)
})
2 changes: 1 addition & 1 deletion test/integration/filesystempublicroutes/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ app.prepare().then(() => {
if (/setAssetPrefix/.test(req.url)) {
app.setAssetPrefix(`http://127.0.0.1:${port}`)
} else if (/setEmptyAssetPrefix/.test(req.url)) {
app.setAssetPrefix(null)
app.setAssetPrefix('')
} else {
// This is to support multi-zones support in localhost
// and may be in staging deployments
Expand Down

0 comments on commit ba31fba

Please sign in to comment.