Skip to content

Commit

Permalink
Add basic test setup
Browse files Browse the repository at this point in the history
  • Loading branch information
arunoda committed May 9, 2017
1 parent e8ff53f commit 7b193f1
Show file tree
Hide file tree
Showing 5 changed files with 234 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ dist
node_modules

# logs
npm-debug.log
*.log

# coverage
.nyc_output
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@
"chromedriver": "2.29.0",
"coveralls": "2.13.1",
"cross-env": "4.0.0",
"express": "4.15.2",
"fly": "2.0.6",
"fly-babel": "2.1.1",
"fly-clear": "1.0.1",
Expand Down
45 changes: 45 additions & 0 deletions test/integration/static/test/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/* global jasmine, describe, it, expect, beforeAll, afterAll */

import { join } from 'path'
import {
nextBuild,
nextExport,
renderViaHTTP,
startStaticServer,
stopApp
} from 'next-test-utils'
import webdriver from 'next-webdriver'

jasmine.DEFAULT_TIMEOUT_INTERVAL = 40000
const appDir = join(__dirname, '../')
const context = {}

describe('Static Export', () => {
beforeAll(async () => {
const outdir = join(appDir, '.out')
await nextBuild(appDir)
await nextExport(appDir, { outdir })

context.server = await startStaticServer(join(appDir, '.out'))
context.port = context.server.address().port
})
afterAll(() => stopApp(context.server))

describe('Render via SSR', () => {
it('should render the home page', async () => {
const html = await renderViaHTTP(context.port, '/')
expect(html).toMatch(/This is the home page/)
})
})

describe('Render via browser', () => {
it('should render the home page', async () => {
const browser = await webdriver(context.port, '/')
const text = await browser
.elementByCss('#home-page p').text()

expect(text).toBe('This is the home page')
browser.close()
})
})
})
16 changes: 15 additions & 1 deletion test/lib/next-test-utils.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import fetch from 'node-fetch'
import qs from 'querystring'
import http from 'http'
import express from 'express'

import server from '../../dist/server/next'
import build from '../../dist/server/build'
import _export from '../../dist/server/export'
import _pkg from '../../package.json'

export const nextServer = server
export const nextBuild = build
export const nextExport = _export
export const pkg = _pkg

export function renderViaAPI (app, pathname, query = {}) {
Expand All @@ -31,7 +34,9 @@ export async function startApp (app) {
}

export async function stopApp (app) {
await server.__app.close()
if (server.__app) {
await server.__app.close()
}
await promiseCall(server, 'close')
}

Expand All @@ -52,3 +57,12 @@ function promiseCall (obj, method, ...args) {
export function waitFor (millis) {
return new Promise((resolve) => setTimeout(resolve, millis))
}

export async function startStaticServer (dir) {
const app = express()
const server = http.createServer(app)
app.use(express.static(dir))

await promiseCall(server, 'listen')
return server
}
Loading

0 comments on commit 7b193f1

Please sign in to comment.