Commit 355c984e authored by Arunoda Susiripala's avatar Arunoda Susiripala Committed by 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
parent e46cedda
...@@ -104,9 +104,6 @@ ...@@ -104,9 +104,6 @@
"jest-cli": "^18.0.0", "jest-cli": "^18.0.0",
"node-fetch": "^1.6.3", "node-fetch": "^1.6.3",
"nyc": "^10.0.0", "nyc": "^10.0.0",
"portfinder": "^1.0.10",
"react": "15.4.2",
"react-dom": "15.4.2",
"run-sequence": "1.2.2", "run-sequence": "1.2.2",
"standard": "8.6.0", "standard": "8.6.0",
"wd": "^1.1.3", "wd": "^1.1.3",
......
...@@ -3,9 +3,10 @@ ...@@ -3,9 +3,10 @@
import { join } from 'path' import { join } from 'path'
import { import {
nextServer, nextServer,
findPort,
renderViaAPI, renderViaAPI,
renderViaHTTP renderViaHTTP,
startApp,
stopApp
} from 'next-test-utils' } from 'next-test-utils'
// test suits // test suits
...@@ -25,11 +26,10 @@ jasmine.DEFAULT_TIMEOUT_INTERVAL = 40000 ...@@ -25,11 +26,10 @@ jasmine.DEFAULT_TIMEOUT_INTERVAL = 40000
describe('Basic Features', () => { describe('Basic Features', () => {
beforeAll(async () => { beforeAll(async () => {
await context.app.prepare() context.server = await startApp(context.app)
context.appPort = await findPort() context.appPort = context.server.address().port
await context.app.start(context.appPort)
}) })
afterAll(() => context.app.close()) afterAll(() => stopApp(context.server))
rendering(context, 'Rendering via API', (p, q) => renderViaAPI(context.app, p, q)) rendering(context, 'Rendering via API', (p, q) => renderViaAPI(context.app, p, q))
rendering(context, 'Rendering via HTTP', (p, q) => renderViaHTTP(context.appPort, p, q)) rendering(context, 'Rendering via HTTP', (p, q) => renderViaHTTP(context.appPort, p, q))
......
...@@ -5,13 +5,15 @@ import { join } from 'path' ...@@ -5,13 +5,15 @@ import { join } from 'path'
import { import {
nextServer, nextServer,
nextBuild, nextBuild,
findPort, startApp,
stopApp,
renderViaHTTP renderViaHTTP
} from 'next-test-utils' } from 'next-test-utils'
const appDir = join(__dirname, '../') const appDir = join(__dirname, '../')
let app
let appPort let appPort
let server
let app
jasmine.DEFAULT_TIMEOUT_INTERVAL = 40000 jasmine.DEFAULT_TIMEOUT_INTERVAL = 40000
describe('Production Usage', () => { describe('Production Usage', () => {
...@@ -23,11 +25,10 @@ describe('Production Usage', () => { ...@@ -23,11 +25,10 @@ describe('Production Usage', () => {
quiet: true quiet: true
}) })
await app.prepare() server = await startApp(app)
appPort = await findPort() appPort = server.address().port
await app.start(appPort)
}) })
afterAll(() => app.close()) afterAll(() => stopApp(server))
describe('With basic usage', () => { describe('With basic usage', () => {
it('should render the page', async () => { it('should render the page', async () => {
......
import portFinder from 'portfinder'
import fetch from 'node-fetch' import fetch from 'node-fetch'
import qs from 'querystring' import qs from 'querystring'
import http from 'http'
import server from '../../dist/server/next' import server from '../../dist/server/next'
import build from '../../dist/server/build' import build from '../../dist/server/build'
...@@ -10,15 +10,6 @@ export const nextServer = server ...@@ -10,15 +10,6 @@ export const nextServer = server
export const nextBuild = build export const nextBuild = build
export const pkg = _pkg 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 = {}) { export function renderViaAPI (app, pathname, query = {}) {
return app.renderToHTML({}, {}, pathname, query) return app.renderToHTML({}, {}, pathname, query)
} }
...@@ -27,3 +18,32 @@ export function renderViaHTTP (appPort, pathname, query = {}) { ...@@ -27,3 +18,32 @@ export function renderViaHTTP (appPort, pathname, query = {}) {
const url = `http://localhost:${appPort}${pathname}?${qs.stringify(query)}` const url = `http://localhost:${appPort}${pathname}?${qs.stringify(query)}`
return fetch(url).then((res) => res.text()) 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)
})
}
This diff is collapsed.
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