Skip to content

Commit

Permalink
Add simple test that custom server works (vercel#44536)
Browse files Browse the repository at this point in the history
- Unify logging in test startup
- Added simple custom server test

It's testing just simple page serving in production mode.

Co-authored-by: Tim Neutkens <[email protected]>
  • Loading branch information
jankaifer and timneutkens committed Jan 11, 2023
1 parent c802a5c commit f61cff3
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 3 deletions.
1 change: 1 addition & 0 deletions test/lib/next-modes/next-dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export class NextDevInstance extends NextInstance {
startArgs = this.startCommand.split(' ')
}

console.log('running', startArgs.join(' '))
await new Promise<void>((resolve, reject) => {
try {
this.childProcess = spawn(startArgs[0], startArgs.slice(1), {
Expand Down
4 changes: 1 addition & 3 deletions test/lib/next-modes/next-start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,8 @@ export class NextStartInstance extends NextInstance {
startArgs = this.startCommand.split(' ')
}

console.log('running', buildArgs.join(' '))
await new Promise<void>((resolve, reject) => {
console.log('running', buildArgs.join(' '))

try {
this.childProcess = spawn(
buildArgs[0],
Expand Down Expand Up @@ -99,7 +98,6 @@ export class NextStartInstance extends NextInstance {
).trim()

console.log('running', startArgs.join(' '))

await new Promise<void>((resolve) => {
try {
this.childProcess = spawn(
Expand Down
15 changes: 15 additions & 0 deletions test/production/custom-server/custom-server.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { createNextDescribe } from 'e2e-utils'

createNextDescribe(
'custom server',
{
files: __dirname,
startCommand: 'node server.js',
},
({ next }) => {
it.each(['a', 'b', 'c'])('can navigate to /%s', async (page) => {
const $ = await next.render$(`/${page}`)
expect($('p').text()).toBe(`Page ${page}`)
})
}
)
3 changes: 3 additions & 0 deletions test/production/custom-server/pages/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function Page() {
return <p id="page-a">Page a</p>
}
3 changes: 3 additions & 0 deletions test/production/custom-server/pages/c.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function Page() {
return <p id="page-c">Page c</p>
}
3 changes: 3 additions & 0 deletions test/production/custom-server/pages/page-b.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function Page() {
return <p id="page-b">Page b</p>
}
36 changes: 36 additions & 0 deletions test/production/custom-server/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const { createServer } = require('http')
const { parse } = require('url')
const next = require('next')

const hostname = 'localhost'
const port = 3000
// when using middleware `hostname` and `port` must be provided below
const app = next({ hostname, port })
const handle = app.getRequestHandler()

app.prepare().then(() => {
createServer(async (req, res) => {
try {
// Be sure to pass `true` as the second argument to `url.parse`.
// This tells it to parse the query portion of the URL.
const parsedUrl = parse(req.url, true)
const { pathname, query } = parsedUrl

if (pathname === '/a') {
await app.render(req, res, '/a', query)
} else if (pathname === '/b') {
await app.render(req, res, '/page-b', query)
} else {
await handle(req, res, parsedUrl)
}
} catch (err) {
console.error('Error occurred handling', req.url, err)
res.statusCode = 500
res.end('internal server error')
}
}).listen(port, (err) => {
if (err) throw err
// Start mode
console.log(`started server on url: http://${hostname}:${port}`)
})
})

0 comments on commit f61cff3

Please sign in to comment.