提交 355c984e 编写于 作者: A Arunoda Susiripala 提交者: Tim Neutkens

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
上级 e46cedda
......@@ -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 () => {
......
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)
})
}
此差异已折叠。
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册