Skip to content

Commit

Permalink
Testing: use a better way to get a port to the test app (#753)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
arunoda authored and timneutkens committed Feb 9, 2017
1 parent e46cedd commit 355c984
Show file tree
Hide file tree
Showing 5 changed files with 324 additions and 326 deletions.
3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
12 changes: 6 additions & 6 deletions test/integration/basic/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
import { join } from 'path'
import {
nextServer,
findPort,
renderViaAPI,
renderViaHTTP
renderViaHTTP,
startApp,
stopApp
} from 'next-test-utils'

// test suits
Expand All @@ -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))
Expand Down
13 changes: 7 additions & 6 deletions test/integration/production/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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 () => {
Expand Down
40 changes: 30 additions & 10 deletions test/lib/next-test-utils.js
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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)
}
Expand All @@ -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

0 comments on commit 355c984

Please sign in to comment.