Unverified Commit db216e00 authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Even more reliable error-recovery tests (#5284)

parent 139bc40f
...@@ -128,7 +128,7 @@ ...@@ -128,7 +128,7 @@
"babel-jest": "23.4.2", "babel-jest": "23.4.2",
"benchmark": "2.1.4", "benchmark": "2.1.4",
"cheerio": "0.22.0", "cheerio": "0.22.0",
"chromedriver": "2.32.3", "chromedriver": "2.42.0",
"clone": "2.1.1", "clone": "2.1.1",
"coveralls": "2.13.1", "coveralls": "2.13.1",
"cross-env": "5.2.0", "cross-env": "5.2.0",
......
/* global describe, it, expect */ /* global describe, it, expect */
import webdriver from 'next-webdriver' import webdriver from 'next-webdriver'
import { waitFor } from 'next-test-utils' import { waitFor, check, File } from 'next-test-utils'
import { readFileSync, writeFileSync } from 'fs' import { readFileSync, writeFileSync } from 'fs'
import { join } from 'path' import { join } from 'path'
...@@ -10,7 +10,7 @@ export default (context, render) => { ...@@ -10,7 +10,7 @@ export default (context, render) => {
it('should have config available on the client', async () => { it('should have config available on the client', async () => {
const browser = await webdriver(context.appPort, '/next-config') const browser = await webdriver(context.appPort, '/next-config')
// Wait for client side to load // Wait for client side to load
await waitFor(5000) await waitFor(10000)
const serverText = await browser.elementByCss('#server-only').text() const serverText = await browser.elementByCss('#server-only').text()
const serverClientText = await browser.elementByCss('#server-and-client').text() const serverClientText = await browser.elementByCss('#server-and-client').text()
...@@ -37,8 +37,7 @@ export default (context, render) => { ...@@ -37,8 +37,7 @@ export default (context, render) => {
// Change the page // Change the page
writeFileSync(pagePath, editedContent, 'utf8') writeFileSync(pagePath, editedContent, 'utf8')
// wait for 5 seconds await waitFor(10000)
await waitFor(5000)
try { try {
// Check whether the this page has reloaded or not. // Check whether the this page has reloaded or not.
...@@ -59,36 +58,47 @@ export default (context, render) => { ...@@ -59,36 +58,47 @@ export default (context, render) => {
}) })
it('should update sass styles using hmr', async () => { it('should update sass styles using hmr', async () => {
const file = new File(join(__dirname, '../', 'components', 'hello-webpack-sass.scss'))
let browser let browser
try { try {
browser = await webdriver(context.appPort, '/webpack-css') browser = await webdriver(context.appPort, '/webpack-css')
const pTag = await browser.elementByCss('.hello-world')
const initialFontSize = await pTag.getComputedCss('color')
expect(initialFontSize).toBe('rgba(255, 255, 0, 1)') expect(
await browser.elementByCss('.hello-world').getComputedCss('color')
).toBe('rgba(255, 255, 0, 1)')
const pagePath = join(__dirname, '../', 'components', 'hello-webpack-sass.scss') file.replace('yellow', 'red')
const originalContent = readFileSync(pagePath, 'utf8') await waitFor(10000)
const editedContent = originalContent.replace('yellow', 'red')
// Change the page await check(
writeFileSync(pagePath, editedContent, 'utf8') async () => {
const tag = await browser.elementByCss('.hello-world')
const prop = await tag.getComputedCss('color')
// wait for 5 seconds expect(prop).toBe('rgba(255, 0, 0, 1)')
await waitFor(5000) return 'works'
},
/works/
)
try { file.restore()
// Check whether the this page has reloaded or not.
const editedPTag = await browser.elementByCss('.hello-world')
const editedFontSize = await editedPTag.getComputedCss('color')
expect(editedFontSize).toBe('rgba(255, 0, 0, 1)') await waitFor(10000)
} finally {
// Finally is used so that we revert the content back to the original regardless of the test outcome await check(
// restore the about page content. async () => {
writeFileSync(pagePath, originalContent, 'utf8') const tag = await browser.elementByCss('.hello-world')
} const prop = await tag.getComputedCss('color')
expect(prop).toBe('rgba(255, 255, 0, 1)')
return 'works'
},
/works/
)
} catch (err) {
file.restore()
throw err
} finally { } finally {
if (browser) { if (browser) {
browser.close() browser.close()
......
/* global describe, it, expect */ /* global describe, it, expect */
import webdriver from 'next-webdriver' import webdriver from 'next-webdriver'
import { waitFor } from 'next-test-utils' import { check } from 'next-test-utils'
export default function (context) { export default function (context) {
describe('Render via browser', () => { describe('Render via browser', () => {
...@@ -100,39 +100,36 @@ export default function (context) { ...@@ -100,39 +100,36 @@ export default function (context) {
.elementByCss('#dynamic-imports-page').click() .elementByCss('#dynamic-imports-page').click()
.waitForElementByCss('#dynamic-imports-page') .waitForElementByCss('#dynamic-imports-page')
// Wait until browser loads the dynamic import chunk await check(
// TODO: We may need to find a better way to do this () => browser.elementByCss('#dynamic-imports-page p').text(),
await waitFor(5000) /Welcome to dynamic imports/
)
const text = await browser
.elementByCss('#dynamic-imports-page p').text()
expect(text).toBe('Welcome to dynamic imports.')
browser.close() browser.close()
}) })
it('should render pages with url hash correctly', async () => { it('should render pages with url hash correctly', async () => {
const browser = await webdriver(context.port, '/') let browser
try {
// Check for the query string content browser = await webdriver(context.port, '/')
const text = await browser
.elementByCss('#with-hash').click()
.waitForElementByCss('#dynamic-page')
.elementByCss('#dynamic-page p').text()
expect(text).toBe('zeit is awesome')
// Check for the hash
while (true) {
const hashText = await browser
.elementByCss('#hash').text()
if (/cool/.test(hashText)) { // Check for the query string content
break const text = await browser
.elementByCss('#with-hash').click()
.waitForElementByCss('#dynamic-page')
.elementByCss('#dynamic-page p').text()
expect(text).toBe('zeit is awesome')
await check(
() => browser.elementByCss('#hash').text(),
/cool/
)
} finally {
if (browser) {
browser.close()
} }
} }
browser.close()
}) })
it('should navigate even if used a button inside <Link />', async () => { it('should navigate even if used a button inside <Link />', async () => {
......
import Link from 'next/link' import Link from 'next/link'
export default () => <div> export default () => <div>
<Link href='/nav/dynamic'><a id='to-dynamic'>To dynamic import</a></Link> <Link href='/nav/dynamic'>
<a id='to-dynamic'>To dynamic import</a>
</Link>
</div> </div>
...@@ -7,7 +7,9 @@ import { ...@@ -7,7 +7,9 @@ import {
findPort, findPort,
launchApp, launchApp,
killApp, killApp,
waitFor waitFor,
check,
getBrowserBodyText
} from 'next-test-utils' } from 'next-test-utils'
const context = {} const context = {}
...@@ -63,12 +65,13 @@ describe('On Demand Entries', () => { ...@@ -63,12 +65,13 @@ describe('On Demand Entries', () => {
let browser let browser
try { try {
browser = await webdriver(context.appPort, '/nav') browser = await webdriver(context.appPort, '/nav')
const text = await browser
.elementByCss('#to-dynamic').click()
.waitForElementByCss('.dynamic-page')
.elementByCss('p').text()
expect(text).toBe('Hello') await browser.eval('document.getElementById("to-dynamic").click()')
await check(async () => {
const text = await getBrowserBodyText(browser)
return text
}, /Hello/)
} finally { } finally {
if (browser) { if (browser) {
browser.close() browser.close()
......
...@@ -152,7 +152,7 @@ export async function startStaticServer (dir) { ...@@ -152,7 +152,7 @@ export async function startStaticServer (dir) {
export async function check (contentFn, regex) { export async function check (contentFn, regex) {
let found = false let found = false
setTimeout(async () => { const timeout = setTimeout(async () => {
if (found) { if (found) {
return return
} }
...@@ -170,6 +170,7 @@ export async function check (contentFn, regex) { ...@@ -170,6 +170,7 @@ export async function check (contentFn, regex) {
const newContent = await contentFn() const newContent = await contentFn()
if (regex.test(newContent)) { if (regex.test(newContent)) {
found = true found = true
clearTimeout(timeout)
break break
} }
await waitFor(1000) await waitFor(1000)
...@@ -231,3 +232,7 @@ export async function getReactErrorOverlayContent (browser) { ...@@ -231,3 +232,7 @@ export async function getReactErrorOverlayContent (browser) {
} }
return browser.eval(`document.querySelector('iframe').contentWindow.document.body.innerHTML`) return browser.eval(`document.querySelector('iframe').contentWindow.document.body.innerHTML`)
} }
export function getBrowserBodyText (browser) {
return browser.eval('document.getElementsByTagName("body")[0].innerText')
}
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