Skip to content

Commit

Permalink
Update tests for BrowserStack (#6810)
Browse files Browse the repository at this point in the history
Update tests to setup webdriver stuff in `jest-environment` and re-use one browser session instead of spawning one for each webdriver call to prevent creating too many BrowserStack sessions.
  • Loading branch information
ijjk authored and timneutkens committed Mar 29, 2019
1 parent 9c2f690 commit 533018f
Show file tree
Hide file tree
Showing 32 changed files with 542 additions and 243 deletions.
13 changes: 13 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ jobs:
JEST_JUNIT_CLASSNAME: '{filepath}'
- store_test_results:
path: ~/repo/reports
test-production:
docker:
- image: circleci/node:8-browsers
working_directory: ~/repo
steps:
- attach_workspace:
at: .
- run:
name: Production Tests
command: '[[ ! -z $BROWSERSTACK_USERNAME ]] && yarn testall test/integration/production/ || echo "Not running for PR"'
deploy:
docker:
- image: circleci/node:8-browsers
Expand All @@ -55,6 +65,9 @@ workflows:
- test:
requires:
- build
- test-production:
requires:
- build
- deploy:
requires:
- test
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ pids
coverage

# test output
test/**/out
test/**/out*
.DS_Store

# Editors
**/.idea

# example output
examples/**/out

2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ module.exports = {
testMatch: ['**/*.test.js'],
verbose: true,
bail: true,
testEnvironment: 'node',
rootDir: 'test',
modulePaths: ['<rootDir>/lib'],
globalSetup: '<rootDir>/jest-global-setup.js',
globalTeardown: '<rootDir>/jest-global-teardown.js',
testEnvironment: '<rootDir>/jest-environment.js',
coverageReporters: ['text', 'lcov', 'cobertura']
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
"babel-core": "7.0.0-bridge.0",
"babel-eslint": "9.0.0",
"babel-jest": "23.6.0",
"browserstack-local": "1.3.7",
"cheerio": "0.22.0",
"chromedriver": "2.46.0",
"clone": "2.1.1",
Expand Down
3 changes: 1 addition & 2 deletions test/integration/amphtml/test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-env jest */
/* global jasmine */
/* global jasmine, webdriver */
import { join } from 'path'
import { readFileSync, writeFileSync } from 'fs'
import {
Expand All @@ -14,7 +14,6 @@ import {
launchApp,
killApp
} from 'next-test-utils'
import webdriver from 'next-webdriver'
import cheerio from 'cheerio'
import amphtmlValidator from 'amphtml-validator'
const appDir = join(__dirname, '../')
Expand Down
3 changes: 1 addition & 2 deletions test/integration/app-aspath/test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* eslint-env jest */
/* global jasmine */
import webdriver from 'next-webdriver'
/* global jasmine, webdriver */
import { readFileSync, writeFileSync } from 'fs'
import { join } from 'path'
import {
Expand Down
2 changes: 1 addition & 1 deletion test/integration/app-document/test/client.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-env jest */
/* global webdriver */

import webdriver from 'next-webdriver'
import { readFileSync, writeFileSync } from 'fs'
import { join } from 'path'
import { check } from 'next-test-utils'
Expand Down
15 changes: 9 additions & 6 deletions test/integration/app-document/test/csp.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
/* eslint-env jest */

import webdriver from 'next-webdriver'
/* global webdriver */

export default (context, render) => {
describe('With CSP enabled', () => {
it('should load inline script by hash', async () => {
const browser = await webdriver(context.appPort, '/?withCSP=hash')
const errLog = await browser.log('browser')
expect(errLog.filter((e) => e.source === 'security')).toEqual([])
if (browser.log) {
const errLog = await browser.log('browser')
expect(errLog.filter((e) => e.source === 'security')).toEqual([])
}
await browser.close()
})

it('should load inline script by nonce', async () => {
const browser = await webdriver(context.appPort, '/?withCSP=nonce')
const errLog = await browser.log('browser')
expect(errLog.filter((e) => e.source === 'security')).toEqual([])
if (browser.log) {
const errLog = await browser.log('browser')
expect(errLog.filter((e) => e.source === 'security')).toEqual([])
}
await browser.close()
})
})
Expand Down
12 changes: 7 additions & 5 deletions test/integration/basic/test/dynamic.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-env jest */
import webdriver from 'next-webdriver'
/* global webdriver */
import cheerio from 'cheerio'
import { waitFor, check } from 'next-test-utils'

Expand Down Expand Up @@ -45,11 +45,13 @@ export default (context, render) => {
await check(() => browser.elementByCss('body').text(), /Nested 2/)
await check(() => browser.elementByCss('body').text(), /Browser hydrated/)

const logs = await browser.log('browser')
if (browser.log) {
const logs = await browser.log('browser')

logs.forEach(logItem => {
expect(logItem.message).not.toMatch(/Expected server HTML to contain/)
})
logs.forEach(logItem => {
expect(logItem.message).not.toMatch(/Expected server HTML to contain/)
})
}
} finally {
if (browser) {
await browser.close()
Expand Down
43 changes: 26 additions & 17 deletions test/integration/basic/test/error-recovery.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-env jest */
import webdriver from 'next-webdriver'
/* global webdriver */
import { join } from 'path'
import { check, File, waitFor, getReactErrorOverlayContent, getBrowserBodyText } from 'next-test-utils'

Expand Down Expand Up @@ -40,9 +40,8 @@ export default (context, renderViaHTTP) => {
it('should have installed the react-overlay-editor editor handler', async () => {
let browser
const aboutPage = new File(join(__dirname, '../', 'pages', 'hmr', 'about.js'))
aboutPage.replace('</div>', 'div')

try {
aboutPage.replace('</div>', 'div')
browser = await webdriver(context.appPort, '/hmr/about')

// react-error-overlay uses the following inline style if an editorHandler is installed
Expand Down Expand Up @@ -76,8 +75,10 @@ export default (context, renderViaHTTP) => {
const aboutPage = new File(join(__dirname, '../', 'pages', 'hmr', 'about.js'))
try {
browser = await webdriver(context.appPort, '/hmr/about')
const text = await browser.elementByCss('p').text()
expect(text).toBe('This is the about page.')
await check(
() => getBrowserBodyText(browser),
/This is the about page/
)

aboutPage.replace('</div>', 'div')

Expand Down Expand Up @@ -147,9 +148,10 @@ export default (context, renderViaHTTP) => {
const aboutPage = new File(join(__dirname, '../', 'pages', 'hmr', 'about.js'))
try {
browser = await webdriver(context.appPort, '/hmr/about')
const text = await browser
.elementByCss('p').text()
expect(text).toBe('This is the about page.')
await check(
() => getBrowserBodyText(browser),
/This is the about page/
)

aboutPage.replace('export', 'aa=20;\nexport')

Expand All @@ -174,9 +176,10 @@ export default (context, renderViaHTTP) => {
const aboutPage = new File(join(__dirname, '../', 'pages', 'hmr', 'about.js'))
try {
browser = await webdriver(context.appPort, '/hmr/about')
const text = await browser.elementByCss('p').text()

expect(text).toBe('This is the about page.')
await check(
() => getBrowserBodyText(browser),
/This is the about page/
)

aboutPage.replace('return', 'throw new Error("an-expected-error");\nreturn')

Expand Down Expand Up @@ -210,8 +213,10 @@ export default (context, renderViaHTTP) => {
const aboutPage = new File(join(__dirname, '../', 'pages', 'hmr', 'about.js'))
try {
browser = await webdriver(context.appPort, '/hmr/about')
const text = await browser.elementByCss('p').text()
expect(text).toBe('This is the about page.')
await check(
() => getBrowserBodyText(browser),
/This is the about page/
)

aboutPage.replace('export default', 'export default {};\nexport const fn =')

Expand Down Expand Up @@ -249,8 +254,10 @@ export default (context, renderViaHTTP) => {
const aboutPage = new File(join(__dirname, '../', 'pages', 'hmr', 'about.js'))
try {
browser = await webdriver(context.appPort, '/hmr/about')
const text = await browser.elementByCss('p').text()
expect(text).toBe('This is the about page.')
await check(
() => getBrowserBodyText(browser),
/This is the about page/
)

aboutPage.replace('export default', 'export default () => /search/;\nexport const fn =')

Expand Down Expand Up @@ -288,8 +295,10 @@ export default (context, renderViaHTTP) => {
const aboutPage = new File(join(__dirname, '../', 'pages', 'hmr', 'about.js'))
try {
browser = await webdriver(context.appPort, '/hmr/about')
const text = await browser.elementByCss('p').text()
expect(text).toBe('This is the about page.')
await check(
() => getBrowserBodyText(browser),
/This is the about page/
)

aboutPage.replace('export default', 'export default undefined;\nexport const fn =')

Expand Down
30 changes: 15 additions & 15 deletions test/integration/basic/test/hmr.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-env jest */
import webdriver from 'next-webdriver'
/* global webdriver */
import { readFileSync, writeFileSync, renameSync, existsSync } from 'fs'
import { join } from 'path'
import { waitFor, check, getBrowserBodyText } from 'next-test-utils'
Expand Down Expand Up @@ -135,15 +135,15 @@ export default (context, renderViaHTTP) => {
// Change the page
writeFileSync(pagePath, editedContent, 'utf8')

// wait for 5 seconds
await waitFor(5000)

try {
// Check whether the this page has reloaded or not.
const editedPTag = await browser.elementByCss('.hmr-style-page p')
const editedFontSize = await editedPTag.getComputedCss('font-size')

expect(editedFontSize).toBe('200px')
await check(
async () => {
const editedPTag = await browser.elementByCss('.hmr-style-page p')
return editedPTag.getComputedCss('font-size')
},
/200px/
)
} finally {
// Finally is used so that we revert the content back to the original regardless of the test outcome
// restore the about page content.
Expand Down Expand Up @@ -173,14 +173,14 @@ export default (context, renderViaHTTP) => {
// Change the page
writeFileSync(pagePath, editedContent, 'utf8')

// wait for 5 seconds
await waitFor(5000)

// Check whether the this page has reloaded or not.
const editedPTag = await browser.elementByCss('.hmr-style-page p')
const editedFontSize = await editedPTag.getComputedCss('font-size')

expect(editedFontSize).toBe('200px')
await check(
async () => {
const editedPTag = await browser.elementByCss('.hmr-style-page p')
return editedPTag.getComputedCss('font-size')
},
/200px/
)
} finally {
if (browser) {
await browser.close()
Expand Down
2 changes: 1 addition & 1 deletion test/integration/basic/test/process-env.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-env jest */
import webdriver from 'next-webdriver'
/* global webdriver */

export default (context) => {
describe('process.env', () => {
Expand Down
3 changes: 1 addition & 2 deletions test/integration/client-404/test/client-navigation.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* eslint-env jest */

import webdriver from 'next-webdriver'
/* global webdriver */

export default (context) => {
describe('Client Navigation 404', () => {
Expand Down
3 changes: 1 addition & 2 deletions test/integration/client-navigation/test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* eslint-env jest */
/* global jasmine */
/* global jasmine, webdriver */
import { join } from 'path'
import webdriver from 'next-webdriver'
import renderingSuite from './rendering'
import {
waitFor,
Expand Down
2 changes: 1 addition & 1 deletion test/integration/config/test/client.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-env jest */
/* global webdriver */

import webdriver from 'next-webdriver'
import { waitFor } from 'next-test-utils' /* check, File */
import { readFileSync, writeFileSync } from 'fs'
import { join } from 'path'
Expand Down
3 changes: 1 addition & 2 deletions test/integration/custom-server/test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
/* eslint-env jest */
/* global jasmine */
/* global jasmine, webdriver */
import { join } from 'path'
import getPort from 'get-port'
import clone from 'clone'
import webdriver from 'next-webdriver'
import {
initNextServerScript,
killApp,
Expand Down
2 changes: 1 addition & 1 deletion test/integration/export/test/browser.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-env jest */
import webdriver from 'next-webdriver'
/* global webdriver */
import { check, getBrowserBodyText } from 'next-test-utils'

export default function (context) {
Expand Down
2 changes: 1 addition & 1 deletion test/integration/export/test/dev.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-env jest */
import webdriver from 'next-webdriver'
/* global webdriver */
import { renderViaHTTP, getBrowserBodyText, check } from 'next-test-utils'
import cheerio from 'cheerio'

Expand Down
3 changes: 1 addition & 2 deletions test/integration/ondemand/test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/* eslint-env jest */
/* global jasmine */
/* global jasmine, webdriver */
import { join, resolve } from 'path'
import { existsSync } from 'fs'
import webdriver from 'next-webdriver'
import AbortController from 'abort-controller'
import {
renderViaHTTP,
Expand Down
2 changes: 1 addition & 1 deletion test/integration/page-extensions/test/hmr.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-env jest */
import webdriver from 'next-webdriver'
/* global webdriver */
import { readFileSync, writeFileSync } from 'fs'
import { join } from 'path'
import { waitFor } from 'next-test-utils'
Expand Down
3 changes: 1 addition & 2 deletions test/integration/production-config/test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-env jest */
/* global jasmine */
/* global jasmine, webdriver */
import { join } from 'path'
import {
nextServer,
Expand All @@ -8,7 +8,6 @@ import {
stopApp,
runNextCommand
} from 'next-test-utils'
import webdriver from 'next-webdriver'

jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 5

Expand Down
2 changes: 1 addition & 1 deletion test/integration/production/test/dynamic.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-env jest */
import webdriver from 'next-webdriver'
/* global webdriver */
import cheerio from 'cheerio'
import { waitFor, check } from 'next-test-utils'

Expand Down
Loading

0 comments on commit 533018f

Please sign in to comment.