Skip to content

Commit

Permalink
Merge pull request #572 from alphagov/add-button-shim
Browse files Browse the repository at this point in the history
Add button shim and setup Puppeteer
  • Loading branch information
alex-ju authored Mar 14, 2018
2 parents e583c8b + 3119acd commit a7488f7
Show file tree
Hide file tree
Showing 8 changed files with 345 additions and 14 deletions.
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",
"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

0 comments on commit a7488f7

Please sign in to comment.