Unverified Commit 9386ba29 authored by Joe Haddad's avatar Joe Haddad Committed by GitHub

Stabilize `config` tests (#13116)

parent f7bdf29e
/* eslint-env jest */ /* eslint-env jest */
import { join } from 'path' import cheerio from 'cheerio'
import { readFileSync, writeFileSync } from 'fs'
import { import {
renderViaHTTP, check,
File,
findPort, findPort,
launchApp,
killApp, killApp,
File, launchApp,
checkExpectations, renderViaHTTP,
} from 'next-test-utils' } from 'next-test-utils'
import fetch from 'node-fetch'
import cheerio from 'cheerio'
import webdriver from 'next-webdriver' import webdriver from 'next-webdriver'
import { readFileSync, writeFileSync } from 'fs' import fetch from 'node-fetch'
import { join } from 'path'
const context = {} const context = {}
jest.setTimeout(1000 * 60 * 5) jest.setTimeout(1000 * 60 * 5)
...@@ -50,10 +50,15 @@ describe('Configuration', () => { ...@@ -50,10 +50,15 @@ describe('Configuration', () => {
let browser let browser
try { try {
browser = await webdriver(context.appPort, '/webpack-css') browser = await webdriver(context.appPort, '/webpack-css')
const fontSize = await browser
.elementByCss('.hello-world') await check(
.getComputedCss('font-size') () => browser.elementByCss('.hello-world').getComputedCss('font-size'),
expect(fontSize).toBe('100px') {
test(content) {
return content === '100px'
},
}
)
} finally { } finally {
if (browser) { if (browser) {
await browser.close() await browser.close()
...@@ -65,10 +70,17 @@ describe('Configuration', () => { ...@@ -65,10 +70,17 @@ describe('Configuration', () => {
let browser let browser
try { try {
browser = await webdriver(context.appPort, '/webpack-css') browser = await webdriver(context.appPort, '/webpack-css')
const backgroundColor = await browser await check(
.elementByCss('.hello-world') () =>
.getComputedCss('background-color') browser
expect(backgroundColor).toBe('rgba(0, 0, 255, 1)') .elementByCss('.hello-world')
.getComputedCss('background-color'),
{
test(content) {
return content === 'rgba(0, 0, 255, 1)'
},
}
)
} finally { } finally {
if (browser) { if (browser) {
await browser.close() await browser.close()
...@@ -115,10 +127,19 @@ describe('Configuration', () => { ...@@ -115,10 +127,19 @@ describe('Configuration', () => {
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('font-size')
expect(initialFontSize).toBe('100px') await check(
async () => {
const pTag = await browser.elementByCss('.hello-world')
const initialFontSize = await pTag.getComputedCss('font-size')
return initialFontSize
},
{
test(content) {
return content === '100px'
},
}
)
const pagePath = join( const pagePath = join(
__dirname, __dirname,
...@@ -134,24 +155,30 @@ describe('Configuration', () => { ...@@ -134,24 +155,30 @@ describe('Configuration', () => {
writeFileSync(pagePath, editedContent, 'utf8') writeFileSync(pagePath, editedContent, 'utf8')
try { try {
await checkExpectations(async () => { // Check whether the this page has reloaded or not.
// Check whether the this page has reloaded or not. await check(
const editedFontSize = await browser () =>
.elementByCss('.hello-world') browser.elementByCss('.hello-world').getComputedCss('font-size'),
.getComputedCss('font-size') {
expect(editedFontSize).toBe('200px') test(content) {
}) return content === '200px'
},
}
)
} finally { } finally {
// Finally is used so that we revert the content back to the original regardless of the test outcome // Finally is used so that we revert the content back to the original regardless of the test outcome
// restore the about page content. // restore the about page content.
writeFileSync(pagePath, originalContent, 'utf8') writeFileSync(pagePath, originalContent, 'utf8')
await checkExpectations(async () => { // This also make sure that the change is reverted when the test ends
// This also make sure that the change is reverted when the etst ends await check(
const editedFontSize = await browser () =>
.elementByCss('.hello-world') browser.elementByCss('.hello-world').getComputedCss('font-size'),
.getComputedCss('font-size') {
expect(editedFontSize).toBe('100px') test(content) {
}) return content === '100px'
},
}
)
} }
} finally { } finally {
if (browser) { if (browser) {
...@@ -167,26 +194,35 @@ describe('Configuration', () => { ...@@ -167,26 +194,35 @@ describe('Configuration', () => {
let browser let browser
try { try {
browser = await webdriver(context.appPort, '/webpack-css') browser = await webdriver(context.appPort, '/webpack-css')
expect( await check(
await browser.elementByCss('.hello-world').getComputedCss('color') () => browser.elementByCss('.hello-world').getComputedCss('color'),
).toBe('rgba(255, 255, 0, 1)') {
test(content) {
return content === 'rgba(255, 255, 0, 1)'
},
}
)
try { try {
file.replace('yellow', 'red') file.replace('yellow', 'red')
await checkExpectations(async () => { await check(
const color = await browser () => browser.elementByCss('.hello-world').getComputedCss('color'),
.elementByCss('.hello-world') {
.getComputedCss('color') test(content) {
expect(color).toBe('rgba(255, 0, 0, 1)') return content === 'rgba(255, 0, 0, 1)'
}) },
}
)
} finally { } finally {
file.restore() file.restore()
await checkExpectations(async () => { await check(
const color = await browser () => browser.elementByCss('.hello-world').getComputedCss('color'),
.elementByCss('.hello-world') {
.getComputedCss('color') test(content) {
expect(color).toBe('rgba(255, 255, 0, 1)') return content === 'rgba(255, 255, 0, 1)'
}) },
}
)
} }
} finally { } finally {
if (browser) { if (browser) {
......
...@@ -365,23 +365,6 @@ export async function check(contentFn, regex, hardError = true) { ...@@ -365,23 +365,6 @@ export async function check(contentFn, regex, hardError = true) {
return false return false
} }
// Runs the provided expectations periodically until they pass or time out
export async function checkExpectations(runExpectations) {
const interval = 250
const timeout = 30000
const startTime = Date.now()
while (true) {
try {
return await runExpectations()
} catch (err) {
if (Date.now() - startTime > timeout) {
throw err
}
}
await waitFor(interval)
}
}
export class File { export class File {
constructor(path) { constructor(path) {
this.path = path this.path = path
......
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