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

feat: integration testing helper #461

Closed
wants to merge 3 commits into from
Closed
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
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"css.js",
"head.js",
"document.js",
"prefetch.js"
"prefetch.js",
"test/helper.js"
],
"bin": {
"next": "./dist/bin/next"
Expand Down Expand Up @@ -62,6 +63,7 @@
"mkdirp-then": "1.2.0",
"mz": "2.6.0",
"path-match": "1.2.4",
"query-string": "^4.2.3",
"react": "15.4.1",
"react-dom": "15.4.1",
"react-hot-loader": "3.0.0-beta.6",
Expand Down
6 changes: 3 additions & 3 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,14 @@ export default class Server {
}

async render (req, res, pathname, query) {
if (this.config.poweredByHeader) {
res.setHeader('X-Powered-By', `Next.js ${pkg.version}`)
}
const html = await this.renderToHTML(req, res, pathname, query)
sendHTML(res, html)
}

async renderToHTML (req, res, pathname, query) {
if (this.config.poweredByHeader) {
res.setHeader('X-Powered-By', `Next.js ${pkg.version}`)
}
if (this.dev) {
const compilationErr = this.getCompilationError(pathname)
if (compilationErr) {
Expand Down
64 changes: 64 additions & 0 deletions test/helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import next from '../dist/server/next'
import {sendHTML} from '../dist/server/render'
import queryString from 'query-string'

const err = Error('App not initialized: please call prepare()')

/*
startup the app used during integration testing.

let app = null
beforeAll(async () => {
app = await setup(dir)
})
*/
let app = null
export async function setup (dir) {
app = next({
dir,
dev: !process.env.SKIP_BUILD,
staticMarkup: true,
quiet: true
})
await app.prepare()
return app
}

/*
after all tests are complete, cleanup the
integration testing app.

afterAll(async () => {
await teardown()
})
*/
export async function teardown () {
if (app) await app.close()
else throw err
}

/*
helper for rendering a page in the integration testing application:

render('/my-awesome-search-page', {q: 'banana'})

req.url is automatically populated.
*/
export async function render (pathname, query = {}, req, res, opts = {}) {
let html = null
if (app) {
req = req || {
headers: {},
url: pathname + '?' + queryString.stringify(query)
}
res = res || {
setHeader: () => {},
end: () => {}
}
html = await app.renderToHTML(req, res, pathname, query, opts)
if (html) sendHTML(res, html)
} else {
throw err
}
return html
}
30 changes: 13 additions & 17 deletions test/integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,22 @@
'use strict'

import { join } from 'path'
import next from '../dist/server/next'
import pkg from '../package.json'
import {setup, render, teardown} from './helper'

const dir = join(__dirname, 'fixtures', 'basic')
const app = next({
dir,
dev: true,
staticMarkup: true,
quiet: true
})

jasmine.DEFAULT_TIMEOUT_INTERVAL = 20000

describe('integration tests', () => {
beforeAll(() => app.prepare())
let app = null
beforeAll(async () => {
app = await setup(dir)
})

afterAll(() => app.close())
afterAll(async () => {
await teardown()
})

test('renders a stateless component', async () => {
const html = await render('/stateless')
Expand Down Expand Up @@ -74,12 +73,13 @@ describe('integration tests', () => {

test('finishes response', async () => {
const res = {
finished: false,
finished: true,
setHeader (key, value) {},
end () {
this.finished = true
}
}
const html = await app.renderToHTML({}, res, '/finish-response', {})
const html = await render('/finish-response', {}, {}, res)
expect(html).toBeFalsy()
})

Expand All @@ -94,7 +94,7 @@ describe('integration tests', () => {
end () {}
}

await app.render(req, res, req.url)
await render(req.url, {}, req, res)
expect(headers['X-Powered-By']).toEqual(`Next.js ${pkg.version}`)
})

Expand All @@ -111,12 +111,8 @@ describe('integration tests', () => {
end () {}
}

await app.render(req, res, req.url)
await render(req.url, req, res)
app.config.poweredByHeader = originalConfigValue
})
})
})

function render (pathname, query = {}) {
return app.renderToHTML({}, {}, pathname, query)
}