mirror of
https://github.com/terribleplan/next.js.git
synced 2024-01-19 02:48:18 +00:00
Testing: use a better way to get a port to the test app (#753)
* Randomize the port returned from test util's findPort(). * Use http's server.listen() to bind to a random available port. * Update yarn.lock * Update yarn.lock
This commit is contained in:
parent
e46cedda57
commit
355c984ed8
|
@ -104,9 +104,6 @@
|
|||
"jest-cli": "^18.0.0",
|
||||
"node-fetch": "^1.6.3",
|
||||
"nyc": "^10.0.0",
|
||||
"portfinder": "^1.0.10",
|
||||
"react": "15.4.2",
|
||||
"react-dom": "15.4.2",
|
||||
"run-sequence": "1.2.2",
|
||||
"standard": "8.6.0",
|
||||
"wd": "^1.1.3",
|
||||
|
|
|
@ -3,9 +3,10 @@
|
|||
import { join } from 'path'
|
||||
import {
|
||||
nextServer,
|
||||
findPort,
|
||||
renderViaAPI,
|
||||
renderViaHTTP
|
||||
renderViaHTTP,
|
||||
startApp,
|
||||
stopApp
|
||||
} from 'next-test-utils'
|
||||
|
||||
// test suits
|
||||
|
@ -25,11 +26,10 @@ jasmine.DEFAULT_TIMEOUT_INTERVAL = 40000
|
|||
|
||||
describe('Basic Features', () => {
|
||||
beforeAll(async () => {
|
||||
await context.app.prepare()
|
||||
context.appPort = await findPort()
|
||||
await context.app.start(context.appPort)
|
||||
context.server = await startApp(context.app)
|
||||
context.appPort = context.server.address().port
|
||||
})
|
||||
afterAll(() => context.app.close())
|
||||
afterAll(() => stopApp(context.server))
|
||||
|
||||
rendering(context, 'Rendering via API', (p, q) => renderViaAPI(context.app, p, q))
|
||||
rendering(context, 'Rendering via HTTP', (p, q) => renderViaHTTP(context.appPort, p, q))
|
||||
|
|
|
@ -5,13 +5,15 @@ import { join } from 'path'
|
|||
import {
|
||||
nextServer,
|
||||
nextBuild,
|
||||
findPort,
|
||||
startApp,
|
||||
stopApp,
|
||||
renderViaHTTP
|
||||
} from 'next-test-utils'
|
||||
|
||||
const appDir = join(__dirname, '../')
|
||||
let app
|
||||
let appPort
|
||||
let server
|
||||
let app
|
||||
jasmine.DEFAULT_TIMEOUT_INTERVAL = 40000
|
||||
|
||||
describe('Production Usage', () => {
|
||||
|
@ -23,11 +25,10 @@ describe('Production Usage', () => {
|
|||
quiet: true
|
||||
})
|
||||
|
||||
await app.prepare()
|
||||
appPort = await findPort()
|
||||
await app.start(appPort)
|
||||
server = await startApp(app)
|
||||
appPort = server.address().port
|
||||
})
|
||||
afterAll(() => app.close())
|
||||
afterAll(() => stopApp(server))
|
||||
|
||||
describe('With basic usage', () => {
|
||||
it('should render the page', async () => {
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import portFinder from 'portfinder'
|
||||
import fetch from 'node-fetch'
|
||||
import qs from 'querystring'
|
||||
import http from 'http'
|
||||
|
||||
import server from '../../dist/server/next'
|
||||
import build from '../../dist/server/build'
|
||||
|
@ -10,15 +10,6 @@ export const nextServer = server
|
|||
export const nextBuild = build
|
||||
export const pkg = _pkg
|
||||
|
||||
export function findPort () {
|
||||
return new Promise((resolve, reject) => {
|
||||
portFinder.getPort((err, port) => {
|
||||
if (err) return reject(err)
|
||||
return resolve(port)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
export function renderViaAPI (app, pathname, query = {}) {
|
||||
return app.renderToHTML({}, {}, pathname, query)
|
||||
}
|
||||
|
@ -27,3 +18,32 @@ export function renderViaHTTP (appPort, pathname, query = {}) {
|
|||
const url = `http://localhost:${appPort}${pathname}?${qs.stringify(query)}`
|
||||
return fetch(url).then((res) => res.text())
|
||||
}
|
||||
|
||||
export async function startApp (app) {
|
||||
await app.prepare()
|
||||
const handler = app.getRequestHandler()
|
||||
const server = http.createServer(handler)
|
||||
server.__app = app
|
||||
|
||||
await promiseCall(server, 'listen')
|
||||
return server
|
||||
}
|
||||
|
||||
export async function stopApp (app) {
|
||||
await server.__app.close()
|
||||
await promiseCall(server, 'close')
|
||||
}
|
||||
|
||||
function promiseCall (obj, method, ...args) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const newArgs = [
|
||||
...args,
|
||||
function (err, res) {
|
||||
if (err) return reject(err)
|
||||
resolve(res)
|
||||
}
|
||||
]
|
||||
|
||||
obj[method](...newArgs)
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue