Skip to content

Commit

Permalink
fix: support several instances of Jest in parallel (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
gregberge authored Sep 24, 2018
1 parent 3fcbaf8 commit 275bc71
Show file tree
Hide file tree
Showing 18 changed files with 24 additions and 33 deletions.
10 changes: 8 additions & 2 deletions jest-puppeteer.config.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
const port = process.env.TEST_SERVER_PORT
? Number(process.env.TEST_SERVER_PORT)
: 4444

process.env.TEST_SERVER_PORT = port

module.exports = {
launch: {
headless: process.env.CI === 'true',
},
server: {
command: 'node server',
port: 4444,
command: `PORT=${port} node server`,
port,
launchTimeout: 4000,
},
}
2 changes: 1 addition & 1 deletion packages/expect-puppeteer/src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const { getDefaultOptions, setDefaultOptions } = require('.')

describe('expect-puppeteer', () => {
beforeEach(async () => {
await page.goto('http://localhost:4444')
await page.goto(`http://localhost:${process.env.TEST_SERVER_PORT}`)
})

it('should work with original Jest matchers', async () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/expect-puppeteer/src/matchers/notToMatch.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
describe('not.toMatch', () => {
beforeEach(async () => {
await page.goto('http://localhost:4444')
await page.goto(`http://localhost:${process.env.TEST_SERVER_PORT}`)
})

describe('Page', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
describe('not.toMatchElement', () => {
beforeEach(async () => {
await page.goto('http://localhost:4444')
await page.goto(`http://localhost:${process.env.TEST_SERVER_PORT}`)
})

describe('Page', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/expect-puppeteer/src/matchers/toClick.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
describe('toClick', () => {
beforeEach(async () => {
await page.goto('http://localhost:4444')
await page.goto(`http://localhost:${process.env.TEST_SERVER_PORT}`)
})

describe('Page', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
describe('toDisplayDialog', () => {
beforeEach(async () => {
await page.goto('http://localhost:4444')
await page.goto(`http://localhost:${process.env.TEST_SERVER_PORT}`)
})

it('should handle dialog', async () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/expect-puppeteer/src/matchers/toFill.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
describe('toFill', () => {
beforeEach(async () => {
await page.goto('http://localhost:4444')
await page.goto(`http://localhost:${process.env.TEST_SERVER_PORT}`)
})

describe('Page', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/expect-puppeteer/src/matchers/toFillForm.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
describe('toFillForm', () => {
beforeEach(async () => {
await page.goto('http://localhost:4444')
await page.goto(`http://localhost:${process.env.TEST_SERVER_PORT}`)
})

describe('Page', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/expect-puppeteer/src/matchers/toMatch.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
describe('toMatch', () => {
beforeEach(async () => {
await page.goto('http://localhost:4444')
await page.goto(`http://localhost:${process.env.TEST_SERVER_PORT}`)
})

describe('Page', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
describe('toMatchElement', () => {
beforeEach(async () => {
await page.goto('http://localhost:4444')
await page.goto(`http://localhost:${process.env.TEST_SERVER_PORT}`)
})

describe('Page', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/expect-puppeteer/src/matchers/toSelect.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
describe('toSelect', () => {
beforeEach(async () => {
await page.goto('http://localhost:4444')
await page.goto(`http://localhost:${process.env.TEST_SERVER_PORT}`)
})

describe('Page', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import path from 'path'

describe('toUploadFile', () => {
beforeEach(async () => {
await page.goto('http://localhost:4444')
await page.goto(`http://localhost:${process.env.TEST_SERVER_PORT}`)
})

describe('Page', () => {
Expand Down
4 changes: 1 addition & 3 deletions packages/jest-environment-puppeteer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
"chalk": "^2.4.1",
"cwd": "^0.10.0",
"jest-dev-server": "^3.3.0",
"lodash": "^4.17.11",
"mkdirp": "^0.5.1",
"rimraf": "^2.6.2"
"lodash": "^4.17.11"
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import fs from 'fs'
// eslint-disable-next-line
import NodeEnvironment from 'jest-environment-node'
import puppeteer from 'puppeteer'
import chalk from 'chalk'
import readConfig from './readConfig'
import { WS_ENDPOINT_PATH } from './constants'

const handleError = error => {
process.emit('uncaughtException', error)
Expand Down Expand Up @@ -32,7 +30,7 @@ class PuppeteerEnvironment extends NodeEnvironment {
const config = await readConfig()
this.global.puppeteerConfig = config

const wsEndpoint = fs.readFileSync(WS_ENDPOINT_PATH, 'utf8')
const wsEndpoint = process.env.PUPPETEER_WS_ENDPOINT
if (!wsEndpoint) {
throw new Error('wsEndpoint not found')
}
Expand Down
5 changes: 0 additions & 5 deletions packages/jest-environment-puppeteer/src/constants.js

This file was deleted.

8 changes: 1 addition & 7 deletions packages/jest-environment-puppeteer/src/global.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
/* eslint-disable no-console */
import fs from 'fs'
import {
setup as setupServer,
teardown as teardownServer,
ERROR_TIMEOUT,
ERROR_NO_COMMAND,
} from 'jest-dev-server'
import mkdirp from 'mkdirp'
import rimraf from 'rimraf'
import puppeteer from 'puppeteer'
import chalk from 'chalk'
import readConfig from './readConfig'
import { DIR, WS_ENDPOINT_PATH } from './constants'

let browser

Expand All @@ -22,8 +18,7 @@ export async function setup() {
} else {
browser = await puppeteer.launch(config.launch)
}
mkdirp.sync(DIR)
fs.writeFileSync(WS_ENDPOINT_PATH, browser.wsEndpoint())
process.env.PUPPETEER_WS_ENDPOINT = browser.wsEndpoint()

if (config.server) {
try {
Expand Down Expand Up @@ -57,5 +52,4 @@ export async function setup() {
export async function teardown() {
await teardownServer()
await browser.close()
rimraf.sync(DIR)
}
2 changes: 1 addition & 1 deletion packages/jest-environment-puppeteer/tests/basic.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
describe('Basic', () => {
beforeAll(async () => {
await page.goto('http://localhost:4444')
await page.goto(`http://localhost:${process.env.TEST_SERVER_PORT}`)
})

it('should display "This is home!" text on page', async () => {
Expand Down
2 changes: 1 addition & 1 deletion server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ const app = express()

app.use(express.static(path.join(__dirname, 'public')))

app.listen(4444)
app.listen(process.env.PORT)

0 comments on commit 275bc71

Please sign in to comment.