Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add simple test that custom server works #44536

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}`)
})
})