Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing: use a better way to get a port to the test app #753

Merged
merged 7 commits into from
Feb 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't remove react and react-dom. Tests fail when you did clean install.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah right, my fault when resolving merge conflicts.

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