Skip to content

Commit 533018f

Browse files
ijjktimneutkens
authored andcommitted
Update tests for BrowserStack (vercel#6810)
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.
1 parent 9c2f690 commit 533018f

32 files changed

Lines changed: 542 additions & 243 deletions

File tree

.circleci/config.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@ jobs:
3131
JEST_JUNIT_CLASSNAME: '{filepath}'
3232
- store_test_results:
3333
path: ~/repo/reports
34+
test-production:
35+
docker:
36+
- image: circleci/node:8-browsers
37+
working_directory: ~/repo
38+
steps:
39+
- attach_workspace:
40+
at: .
41+
- run:
42+
name: Production Tests
43+
command: '[[ ! -z $BROWSERSTACK_USERNAME ]] && yarn testall test/integration/production/ || echo "Not running for PR"'
3444
deploy:
3545
docker:
3646
- image: circleci/node:8-browsers
@@ -55,6 +65,9 @@ workflows:
5565
- test:
5666
requires:
5767
- build
68+
- test-production:
69+
requires:
70+
- build
5871
- deploy:
5972
requires:
6073
- test

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ pids
1818
coverage
1919

2020
# test output
21-
test/**/out
21+
test/**/out*
2222
.DS_Store
2323

2424
# Editors
2525
**/.idea
2626

2727
# example output
2828
examples/**/out
29+

jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ module.exports = {
44
testMatch: ['**/*.test.js'],
55
verbose: true,
66
bail: true,
7-
testEnvironment: 'node',
87
rootDir: 'test',
98
modulePaths: ['<rootDir>/lib'],
109
globalSetup: '<rootDir>/jest-global-setup.js',
1110
globalTeardown: '<rootDir>/jest-global-teardown.js',
11+
testEnvironment: '<rootDir>/jest-environment.js',
1212
coverageReporters: ['text', 'lcov', 'cobertura']
1313
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
"babel-core": "7.0.0-bridge.0",
7171
"babel-eslint": "9.0.0",
7272
"babel-jest": "23.6.0",
73+
"browserstack-local": "1.3.7",
7374
"cheerio": "0.22.0",
7475
"chromedriver": "2.46.0",
7576
"clone": "2.1.1",

test/integration/amphtml/test/index.test.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-env jest */
2-
/* global jasmine */
2+
/* global jasmine, webdriver */
33
import { join } from 'path'
44
import { readFileSync, writeFileSync } from 'fs'
55
import {
@@ -14,7 +14,6 @@ import {
1414
launchApp,
1515
killApp
1616
} from 'next-test-utils'
17-
import webdriver from 'next-webdriver'
1817
import cheerio from 'cheerio'
1918
import amphtmlValidator from 'amphtml-validator'
2019
const appDir = join(__dirname, '../')

test/integration/app-aspath/test/index.test.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/* eslint-env jest */
2-
/* global jasmine */
3-
import webdriver from 'next-webdriver'
2+
/* global jasmine, webdriver */
43
import { readFileSync, writeFileSync } from 'fs'
54
import { join } from 'path'
65
import {

test/integration/app-document/test/client.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-env jest */
2+
/* global webdriver */
23

3-
import webdriver from 'next-webdriver'
44
import { readFileSync, writeFileSync } from 'fs'
55
import { join } from 'path'
66
import { check } from 'next-test-utils'

test/integration/app-document/test/csp.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
/* eslint-env jest */
2-
3-
import webdriver from 'next-webdriver'
2+
/* global webdriver */
43

54
export default (context, render) => {
65
describe('With CSP enabled', () => {
76
it('should load inline script by hash', async () => {
87
const browser = await webdriver(context.appPort, '/?withCSP=hash')
9-
const errLog = await browser.log('browser')
10-
expect(errLog.filter((e) => e.source === 'security')).toEqual([])
8+
if (browser.log) {
9+
const errLog = await browser.log('browser')
10+
expect(errLog.filter((e) => e.source === 'security')).toEqual([])
11+
}
1112
await browser.close()
1213
})
1314

1415
it('should load inline script by nonce', async () => {
1516
const browser = await webdriver(context.appPort, '/?withCSP=nonce')
16-
const errLog = await browser.log('browser')
17-
expect(errLog.filter((e) => e.source === 'security')).toEqual([])
17+
if (browser.log) {
18+
const errLog = await browser.log('browser')
19+
expect(errLog.filter((e) => e.source === 'security')).toEqual([])
20+
}
1821
await browser.close()
1922
})
2023
})

test/integration/basic/test/dynamic.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-env jest */
2-
import webdriver from 'next-webdriver'
2+
/* global webdriver */
33
import cheerio from 'cheerio'
44
import { waitFor, check } from 'next-test-utils'
55

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

48-
const logs = await browser.log('browser')
48+
if (browser.log) {
49+
const logs = await browser.log('browser')
4950

50-
logs.forEach(logItem => {
51-
expect(logItem.message).not.toMatch(/Expected server HTML to contain/)
52-
})
51+
logs.forEach(logItem => {
52+
expect(logItem.message).not.toMatch(/Expected server HTML to contain/)
53+
})
54+
}
5355
} finally {
5456
if (browser) {
5557
await browser.close()

test/integration/basic/test/error-recovery.js

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-env jest */
2-
import webdriver from 'next-webdriver'
2+
/* global webdriver */
33
import { join } from 'path'
44
import { check, File, waitFor, getReactErrorOverlayContent, getBrowserBodyText } from 'next-test-utils'
55

@@ -40,9 +40,8 @@ export default (context, renderViaHTTP) => {
4040
it('should have installed the react-overlay-editor editor handler', async () => {
4141
let browser
4242
const aboutPage = new File(join(__dirname, '../', 'pages', 'hmr', 'about.js'))
43-
aboutPage.replace('</div>', 'div')
44-
4543
try {
44+
aboutPage.replace('</div>', 'div')
4645
browser = await webdriver(context.appPort, '/hmr/about')
4746

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

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

@@ -147,9 +148,10 @@ export default (context, renderViaHTTP) => {
147148
const aboutPage = new File(join(__dirname, '../', 'pages', 'hmr', 'about.js'))
148149
try {
149150
browser = await webdriver(context.appPort, '/hmr/about')
150-
const text = await browser
151-
.elementByCss('p').text()
152-
expect(text).toBe('This is the about page.')
151+
await check(
152+
() => getBrowserBodyText(browser),
153+
/This is the about page/
154+
)
153155

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

@@ -174,9 +176,10 @@ export default (context, renderViaHTTP) => {
174176
const aboutPage = new File(join(__dirname, '../', 'pages', 'hmr', 'about.js'))
175177
try {
176178
browser = await webdriver(context.appPort, '/hmr/about')
177-
const text = await browser.elementByCss('p').text()
178-
179-
expect(text).toBe('This is the about page.')
179+
await check(
180+
() => getBrowserBodyText(browser),
181+
/This is the about page/
182+
)
180183

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

@@ -210,8 +213,10 @@ export default (context, renderViaHTTP) => {
210213
const aboutPage = new File(join(__dirname, '../', 'pages', 'hmr', 'about.js'))
211214
try {
212215
browser = await webdriver(context.appPort, '/hmr/about')
213-
const text = await browser.elementByCss('p').text()
214-
expect(text).toBe('This is the about page.')
216+
await check(
217+
() => getBrowserBodyText(browser),
218+
/This is the about page/
219+
)
215220

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

@@ -249,8 +254,10 @@ export default (context, renderViaHTTP) => {
249254
const aboutPage = new File(join(__dirname, '../', 'pages', 'hmr', 'about.js'))
250255
try {
251256
browser = await webdriver(context.appPort, '/hmr/about')
252-
const text = await browser.elementByCss('p').text()
253-
expect(text).toBe('This is the about page.')
257+
await check(
258+
() => getBrowserBodyText(browser),
259+
/This is the about page/
260+
)
254261

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

@@ -288,8 +295,10 @@ export default (context, renderViaHTTP) => {
288295
const aboutPage = new File(join(__dirname, '../', 'pages', 'hmr', 'about.js'))
289296
try {
290297
browser = await webdriver(context.appPort, '/hmr/about')
291-
const text = await browser.elementByCss('p').text()
292-
expect(text).toBe('This is the about page.')
298+
await check(
299+
() => getBrowserBodyText(browser),
300+
/This is the about page/
301+
)
293302

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

0 commit comments

Comments
 (0)