Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Improve test setup #1372

Merged
merged 8 commits into from
Mar 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions .babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@
"transform-runtime"
],
"env": {
"test-build": {
"plugins": [
"istanbul"
]
},
"test": {
"presets": [
"es2015",
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
"pretestonly": "fly pretest",
"testonly": "cross-env NODE_PATH=test/lib jest \\.test.js",
"posttestonly": "fly posttest",
"pretest": "npm run lint && cross-env NODE_ENV=test-build npm run release",
"test": "npm run testonly -- --coverage --forceExit",
"pretest": "npm run lint",
"test": "npm run testonly -- --coverage --forceExit --runInBand --verbose --bail",
"coveralls": "nyc --instrument=false --source-map=false report --temp-directory=./coverage --reporter=text-lcov | coveralls",
"lint": "standard 'bin/*' 'client/**/*.js' 'examples/**/*.js' 'lib/**/*.js' 'pages/**/*.js' 'server/**/*.js' 'test/**/*.js'",
"prepublish": "npm run release",
Expand Down Expand Up @@ -90,7 +90,7 @@
"devDependencies": {
"babel-eslint": "7.1.1",
"babel-jest": "18.0.0",
"babel-plugin-istanbul": "3.0.0",
"babel-plugin-istanbul": "4.0.0",
"babel-plugin-transform-remove-strict-mode": "0.0.2",
"babel-preset-es2015": "6.22.0",
"benchmark": "2.1.3",
Expand Down
30 changes: 15 additions & 15 deletions test/integration/basic/test/client-navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default (context, render) => {
.elementByCss('p').text()

expect(text).toBe('This is the about page.')
await browser.close()
browser.close()
})

it('should navigate via the client side', async () => {
Expand All @@ -28,7 +28,7 @@ export default (context, render) => {
.elementByCss('#counter').text()

expect(counterText).toBe('Counter: 1')
await browser.close()
browser.close()
})
})

Expand All @@ -41,7 +41,7 @@ export default (context, render) => {
.elementByCss('p').text()

expect(text).toBe('This is the home.')
await browser.close()
browser.close()
})
})

Expand All @@ -56,7 +56,7 @@ export default (context, render) => {
const expectedErrorMessage = '"EmptyInitialPropsPage.getInitialProps()" should resolve to an object. But found "null" instead.'
expect(preText.includes(expectedErrorMessage)).toBeTruthy()

await browser.close()
browser.close()
})
})

Expand All @@ -69,7 +69,7 @@ export default (context, render) => {
.elementByCss('p').text()

expect(text).toBe('2')
await browser.close()
browser.close()
})

it('should remove querystring', async () => {
Expand All @@ -80,7 +80,7 @@ export default (context, render) => {
.elementByCss('p').text()

expect(text).toBe('0')
await browser.close()
browser.close()
})
})

Expand All @@ -95,7 +95,7 @@ export default (context, render) => {
.elementByCss('p').text()

expect(countAfterClicked).toBe('COUNT: 1')
await browser.close()
browser.close()
})

it('should always replace the state', async () => {
Expand All @@ -116,7 +116,7 @@ export default (context, render) => {
.back()
.waitForElementByCss('.nav-home')

await browser.close()
browser.close()
})
})

Expand All @@ -131,7 +131,7 @@ export default (context, render) => {

expect(counter).toBe('COUNT: 0')

await browser.close()
browser.close()
})
})

Expand All @@ -145,7 +145,7 @@ export default (context, render) => {

expect(counter).toBe('COUNT: 0')

await browser.close()
browser.close()
})
})

Expand All @@ -160,7 +160,7 @@ export default (context, render) => {

expect(counter).toBe('COUNT: 1')

await browser.close()
browser.close()
})
})

Expand All @@ -175,7 +175,7 @@ export default (context, render) => {

expect(counter).toBe('COUNT: 0')

await browser.close()
browser.close()
})
})
})
Expand All @@ -193,7 +193,7 @@ export default (context, render) => {
.elementByCss('#get-initial-props-run-count').text()
expect(getInitialPropsRunCount).toBe('getInitialProps run count: 1')

await browser.close()
browser.close()
})

it('should handle the back button and should not run getInitialProps', async () => {
Expand All @@ -213,7 +213,7 @@ export default (context, render) => {
.elementByCss('#get-initial-props-run-count').text()
expect(getInitialPropsRunCount).toBe('getInitialProps run count: 1')

await browser.close()
browser.close()
})

it('should run getInitialProps always when rending the page to the screen', async () => {
Expand All @@ -233,7 +233,7 @@ export default (context, render) => {
.elementByCss('#get-initial-props-run-count').text()
expect(getInitialPropsRunCount).toBe('getInitialProps run count: 2')

await browser.close()
browser.close()
})
})
})
Expand Down
46 changes: 44 additions & 2 deletions test/lib/next-webdriver.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,51 @@ import wd from 'wd'

export default async function (appPort, pathname) {
const url = `http://localhost:${appPort}${pathname}`
console.log(`> Start loading browser with url: ${url}`)

// Sometimes browser won't initialize due to some random issues.
// So, we need to timeout the initialization and retry again.
for (let lc = 0; lc < 5; lc++) {
try {
const browser = await getBrowser(url, 5000)
console.log(`> Complete loading browser with url: ${url}`)
return browser
} catch (ex) {
console.warn(`> Error when loading browser with url: ${url}`)
if (ex.message === 'TIMEOUT') continue
throw ex
}
}

console.error(`> Tried 5 times. Cannot load the browser for url: ${url}`)
throw new Error(`Couldn't start the browser for url: ${url}`)
}

function getBrowser (url, timeout) {
const browser = wd.promiseChainRemote('http://localhost:9515/')
await browser.init({browserName: 'chrome'}).get(url)

return browser
return new Promise((resolve, reject) => {
let timeouted = false
const timeoutHandler = setTimeout(() => {
timeouted = true
const error = new Error('TIMEOUT')
reject(error)
}, timeout)

browser.init({browserName: 'chrome'}).get(url, (err) => {
if (timeouted) {
browser.close()
return
}

clearTimeout(timeoutHandler)

if (err) {
reject(err)
return
}

resolve(browser)
})
})
}
18 changes: 9 additions & 9 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,15 @@ babel-plugin-check-es2015-constants@^6.22.0:
dependencies:
babel-runtime "^6.22.0"

[email protected], babel-plugin-istanbul@^3.0.0:
[email protected], babel-plugin-istanbul@^4.0.0:
version "4.0.0"
resolved "https://registry.npmjs.org/babel-plugin-istanbul/-/babel-plugin-istanbul-4.0.0.tgz#36bde8fbef4837e5ff0366531a2beabd7b1ffa10"
dependencies:
find-up "^2.1.0"
istanbul-lib-instrument "^1.4.2"
test-exclude "^4.0.0"

babel-plugin-istanbul@^3.0.0:
version "3.0.0"
resolved "https://registry.npmjs.org/babel-plugin-istanbul/-/babel-plugin-istanbul-3.0.0.tgz#da7324520ae0b8a44b6a078e72e883374a9fab76"
dependencies:
Expand All @@ -476,14 +484,6 @@ [email protected], babel-plugin-istanbul@^3.0.0:
object-assign "^4.1.0"
test-exclude "^3.2.2"

babel-plugin-istanbul@^4.0.0:
version "4.0.0"
resolved "https://registry.npmjs.org/babel-plugin-istanbul/-/babel-plugin-istanbul-4.0.0.tgz#36bde8fbef4837e5ff0366531a2beabd7b1ffa10"
dependencies:
find-up "^2.1.0"
istanbul-lib-instrument "^1.4.2"
test-exclude "^4.0.0"

babel-plugin-jest-hoist@^18.0.0:
version "18.0.0"
resolved "https://registry.npmjs.org/babel-plugin-jest-hoist/-/babel-plugin-jest-hoist-18.0.0.tgz#4150e70ecab560e6e7344adc849498072d34e12a"
Expand Down