Commit 7b193f1a authored by Arunoda Susiripala's avatar Arunoda Susiripala

Add basic test setup

parent e8ff53f5
......@@ -6,7 +6,7 @@ dist
node_modules
# logs
npm-debug.log
*.log
# coverage
.nyc_output
......
......@@ -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",
......
/* 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()
})
})
})
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 = {}) {
......@@ -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')
}
......@@ -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
}
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