Unverified Commit 7fe9e035 authored by Jan Kaifer's avatar Jan Kaifer Committed by GitHub

Add simple test that custom server works (#44536)

- Unify logging in test startup
- Added simple custom server test

It's testing just simple page serving in production mode.
Co-authored-by: 's avatarTim Neutkens <6324199+timneutkens@users.noreply.github.com>
parent 86f384fc
...@@ -35,6 +35,7 @@ export class NextDevInstance extends NextInstance { ...@@ -35,6 +35,7 @@ export class NextDevInstance extends NextInstance {
startArgs = this.startCommand.split(' ') startArgs = this.startCommand.split(' ')
} }
console.log('running', startArgs.join(' '))
await new Promise<void>((resolve, reject) => { await new Promise<void>((resolve, reject) => {
try { try {
this.childProcess = spawn(startArgs[0], startArgs.slice(1), { this.childProcess = spawn(startArgs[0], startArgs.slice(1), {
......
...@@ -63,9 +63,8 @@ export class NextStartInstance extends NextInstance { ...@@ -63,9 +63,8 @@ export class NextStartInstance extends NextInstance {
startArgs = this.startCommand.split(' ') startArgs = this.startCommand.split(' ')
} }
console.log('running', buildArgs.join(' '))
await new Promise<void>((resolve, reject) => { await new Promise<void>((resolve, reject) => {
console.log('running', buildArgs.join(' '))
try { try {
this.childProcess = spawn( this.childProcess = spawn(
buildArgs[0], buildArgs[0],
...@@ -99,7 +98,6 @@ export class NextStartInstance extends NextInstance { ...@@ -99,7 +98,6 @@ export class NextStartInstance extends NextInstance {
).trim() ).trim()
console.log('running', startArgs.join(' ')) console.log('running', startArgs.join(' '))
await new Promise<void>((resolve) => { await new Promise<void>((resolve) => {
try { try {
this.childProcess = spawn( this.childProcess = spawn(
......
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}`)
})
}
)
export default function Page() {
return <p id="page-a">Page a</p>
}
export default function Page() {
return <p id="page-c">Page c</p>
}
export default function Page() {
return <p id="page-b">Page b</p>
}
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}`)
})
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment