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

Add button shim and setup Puppeteer #572

Merged
merged 6 commits into from
Mar 14, 2018
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
11 changes: 0 additions & 11 deletions app/__tests__/app.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
const request = require('request')
const cheerio = require('cheerio')

const app = require('../app.js')
const lib = require('../../lib/file-helper')

const requestParamsHomepage = {
Expand Down Expand Up @@ -63,16 +62,6 @@ const requestParamsExampleTypography = {
}

describe('frontend app', () => {
let server

beforeAll(done => {
server = app.listen(3000, done)
})

afterAll(done => {
server.close(done)
})

describe('homepage', () => {
it('should resolve with a http status code of 200', done => {
request.get(requestParamsHomepage, (err, res) => {
Expand Down
33 changes: 33 additions & 0 deletions lib/puppeteer/environment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const chalk = require('chalk')
const NodeEnvironment = require('jest-environment-node')
const puppeteer = require('puppeteer')
const fs = require('fs')
const os = require('os')
const path = require('path')

const DIR = path.join(os.tmpdir(), 'jest-puppeteer-global-setup')

class PuppeteerEnvironment extends NodeEnvironment {
async setup () {
console.log(chalk.yellow('Setup Test Environment.'))
await super.setup()
const wsEndpoint = fs.readFileSync(path.join(DIR, 'wsEndpoint'), 'utf8')
if (!wsEndpoint) {
throw new Error('wsEndpoint not found')
}
this.global.__BROWSER__ = await puppeteer.connect({
browserWSEndpoint: wsEndpoint
})
}

async teardown () {
console.log(chalk.yellow('Teardown Test Environment.'))
await super.teardown()
}

runScript (script) {
return super.runScript(script)
}
}

module.exports = PuppeteerEnvironment
22 changes: 22 additions & 0 deletions lib/puppeteer/setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const chalk = require('chalk')
const puppeteer = require('puppeteer')
const fs = require('fs')
const mkdirp = require('mkdirp')
const os = require('os')
const path = require('path')
const app = require('../../app/app.js')

const DIR = path.join(os.tmpdir(), 'jest-puppeteer-global-setup')

module.exports = async function () {
console.log(chalk.green('\nStart server'))
global.__SERVER__ = app.listen(3000)
console.log(chalk.green('Setup Puppeteer'))//
// we use --no-sandbox --disable-setuid-sandbox as a workaround for the
// 'No usable sandbox! Update your kernel' error
// see more https://github.com/Googlechrome/puppeteer/issues/290
const browser = await puppeteer.launch({args: ['--no-sandbox', '--disable-setuid-sandbox']})
global.__BROWSER__ = browser
mkdirp.sync(DIR)
fs.writeFileSync(path.join(DIR, 'wsEndpoint'), browser.wsEndpoint())
}
14 changes: 14 additions & 0 deletions lib/puppeteer/teardown.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const chalk = require('chalk')
const rimraf = require('rimraf')
const os = require('os')
const path = require('path')

const DIR = path.join(os.tmpdir(), 'jest-puppeteer-global-setup')

module.exports = async function () {
console.log(chalk.green('Teardown Puppeteer'))
await global.__BROWSER__.close()
console.log(chalk.green('Close server'))
await global.__SERVER__.close()
rimraf.sync(DIR)
}
133 changes: 133 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
"build:dist": "node bin/check-nvmrc.js && gulp build:dist --destination 'dist' && npm run test:build:dist",
"test": "standard && gulp test && npm run test:app && npm run test:components && npm run test:generate:readme",
"test:app": "jest app/__tests__/app.test.js --forceExit # express server fails to end process",
"test:components": "jest src/ && jest tasks/gulp/__tests__/check-individual-components-compile.test.js",
"test:generate:readme": "jest tasks/gulp/__tests__/check-generate-readme.test.js",
"test:components": "gulp copy-assets && jest src/ --forceExit && jest tasks/gulp/__tests__/check-individual-components-compile.test.js --forceExit",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we document why we need to use --forceExit here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is 'documented' on line 24. would be good to look into is and see if we can make it work without --forceExit, but maybe not part of this PR

"test:generate:readme": "jest tasks/gulp/__tests__/check-generate-readme.test.js --forceExit",
"test:build:packages": "jest tasks/gulp/__tests__/after-build-packages.test.js",
"test:build:dist": "jest tasks/gulp/__tests__/after-build-dist.test.js"
},
Expand Down Expand Up @@ -62,6 +62,7 @@
"oldie": "^1.3.0",
"postcss-normalize": "^3.0.0",
"postcss-pseudo-classes": "^0.2.0",
"puppeteer": "^1.1.1",
"request": "^2.83.0",
"run-sequence": "^2.2.0",
"standard": "^10.0.2",
Expand All @@ -86,6 +87,8 @@
"setupTestFrameworkScriptFile": "./config/jest-setup.js",
"snapshotSerializers": [
"jest-serializer-html"
]
],
"globalSetup": "./lib/puppeteer/setup.js",
"globalTeardown": "./lib/puppeteer/teardown.js"
}
}
Loading