Package detail

jest-environment-puppeteer-jsdom

zaqqaz521MIT6.0.0

Puppeteer environment for Jest.

jest, jest-environment, puppeteer, jest-puppeteer

readme

jest-environment-puppeteer

Build Status version MIT License

Run your tests using Jest & Puppeteer 🎪✨

npm install jest-environment-puppeteer puppeteer

Usage

Update your Jest configuration:

{
  "globalSetup": "jest-environment-puppeteer/setup",
  "globalTeardown": "jest-environment-puppeteer/teardown",
  "testEnvironment": "jest-environment-puppeteer"
}

Use Puppeteer in your tests:

describe('Google', () => {
  beforeAll(async () => {
    await page.goto('https://google.com')
  })

  it('should display "google" text on page', async () => {
    const text = await page.evaluate(() => document.body.textContent)
    expect(text).toContain('google')
  })
})

API

global.browser

Give access to the Puppeteer Browser.

it('should open a new page', async () => {
  const page = await browser.newPage()
  await page.goto('https://google.com')
})

global.page

Give access to a Puppeteer Page opened at start (you will use it most of time).

it('should fill an input', async () => {
  await page.type('#myinput', 'Hello')
})

global.context

Give access to a browser context that is instantiated when the browser is launched. You can control whether each test has its own isolated browser context using the browserContext option in your jest-puppeteer.config.js.

global.jestPuppeteer.debug()

Put test in debug mode.

  • Jest is suspended (no timeout)
  • A debugger instruction to Chromium, if Puppeteer has been launched with { devtools: true } it will stop
it('should put test in debug mode', async () => {
  await jestPuppeteer.debug()
})

global.jestPuppeteer.resetPage()

Reset global.page

beforeEach(async () => {
  await jestPuppeteer.resetPage()
})

global.jestPuppeteer.resetBrowser()

Reset global.browser, global.context, and global.page

beforeEach(async () => {
  await jestPuppeteer.resetBrowser()
})

jest-puppeteer.config.js

You can specify a jest-puppeteer.config.js at the root of the project or define a custom path using JEST_PUPPETEER_CONFIG environment variable. It should export a config object or a Promise for a config object.

  • launch <[object]> All Puppeteer launch options can be specified in config. Since it is JavaScript, you can use all stuff you need, including environment.
  • connect <[object]> All Puppeteer connect options can be specified in config. This is an alternative to launch config, allowing you to connect to an already running instance of Chrome.
  • browserContext <[string]>. By default, the browser context (cookies, localStorage, etc) is shared between all tests. The following options are available for browserContext:
    • default Each test starts a tab, so all tests share the same context.
    • incognito Each tests starts an incognito window, so all tests have a separate, isolated context. Useful when running tests that could interfere with one another. (Example: testing multiple users on the same app at once with login, transactions, etc.)
  • exitOnPageError <[boolean]> Exits page on any global error message thrown. Defaults to true.
  • server <[Object]> Server options allowed by jest-dev-server

Example 1

// jest-puppeteer.config.js
module.exports = {
  launch: {
    dumpio: true,
    headless: process.env.HEADLESS !== 'false',
  },
  server: {
    command: 'node server.js',
    port: 4444,
    launchTimeout: 10000,
    debug: true,
  },
}

Example 2

This example uses an already running instance of Chrome by passing the active web socket endpoint to connect. This is useful, for example, when you want to connect to Chrome running in the cloud.

// jest-puppeteer.config.js
const fetch = require('node-fetch')
const dockerHost = 'http://localhost:9222'

async function getConfig() {
  const response = await fetch(`${dockerHost}/json/version`)
  const browserWSEndpoint = (await response.json()).webSocketDebuggerUrl
  return {
    connect: {
      browserWSEndpoint,
    },
    server: {
      command: 'node server.js',
      port: 3000,
      launchTimeout: 10000,
      debug: true,
    },
  }
}

module.exports = getConfig()

Inspiration

Thanks to Fumihiro Xue for his great Jest example.

License

MIT

changelog

Change Log

All notable changes to this project will be documented in this file. See Conventional Commits for commit guidelines.

6.0.0 (2021-09-23)

Bug Fixes

5.0.4 (2021-05-26)

Bug Fixes

5.0.3 (2021-04-28)

Bug Fixes

5.0.1 (2021-04-19)

Bug Fixes

  • add jest-environment-node as a dependency (#397) (11f2e38)

5.0.0 (2021-04-16)

Bug Fixes

  • Leverage Puppeteer's native support for Firefox (#356) (e54fb7e)
  • require to puppeteer-core as fallback (#315) (49d313c)

BREAKING CHANGES

  • browser config is deprecated. Use launch.product instead.

Co-authored-by: Tony Brix tony@brix.ninja

4.4.0 (2019-12-18)

Bug Fixes

4.3.0 (2019-07-14)

Features

  • add jestPuppeteer.resetBrowser method (#237) (ae93739)

4.2.0 (2019-05-28)

Features

4.1.1 (2019-04-11)

Note: Version bump only for package jest-environment-puppeteer

4.1.0 (2019-03-14)

Bug Fixes

  • invalid connect options, when using browserURL (#212) (6e483e6)

4.0.0 (2019-02-13)

Bug Fixes

  • Cannot read property watch of undefined in jest v22 version (#197) (db731a3)

3.9.0 (2019-01-22)

Bug Fixes

  • jest --watch fails to reload (#182) (cc9bbfa)
  • wrong timeout when using jestPuppeteer.debug() (#185) (0f4c720)

Features

3.8.0 (2019-01-11)

Bug Fixes

  • disconnect the browser on environment teardown (#173) (5d073cc)

Features

  • jest-environment-puppeteer: accept a promise as config (#178) (40bc3a2)

3.7.0 (2018-12-11)

Note: Version bump only for package jest-environment-puppeteer

3.6.0 (2018-12-06)

Bug Fixes

  • skip closing external browser on teardown (0505b2c)
  • use browser.disconnect method (7afbb2e)

3.5.2 (2018-11-27)

Note: Version bump only for package jest-environment-puppeteer

3.5.1 (2018-11-11)

Note: Version bump only for package jest-environment-puppeteer

3.5.0 (2018-11-04)

Features

Performance Improvements

3.4.0 (2018-09-24)

Bug Fixes

  • support several instances of Jest in parallel (#138) (275bc71)

Features

  • connect to an already existing instance of Chrome (#100) (3fcbaf8)

3.3.1 (2018-08-17)

Bug Fixes

  • revert pid based endpoint file (4a08847), closes #103

3.3.0 (2018-08-15)

Features

  • add support for defaultViewport option (7b484a8)
  • support multiple processes on same machine (#103) (4d37d17)

3.2.1 (2018-06-17)

Bug Fixes

3.2.0 (2018-06-17)

Features

3.1.0 (2018-06-16)

Features

  • jest-dev-server: externalize it & auto-kill (45e8fbb), closes #66 #68

2.4.0 (2018-04-24)

Features

  • adjust default timeout with slowMo (6871ec8), closes #36
  • jest-environment-puppeteer: add server.launchTimeout & server.debug options (e312717), closes #44
  • jest-environment-puppeteer: added config option for global err msg (#35) (d87c99a), closes #34

2.3.0 (2018-04-06)

Features

  • jest-environment-puppeteer: support custom config (6cd3050), closes #19

2.2.3 (2018-04-03)

Bug Fixes

  • support ignoreHTTPSErrors launch option (ed60439)

2.2.2 (2018-04-01)

Bug Fixes

  • jest-environment-puppeteer: support slowMo (0fd5b19), closes #20

2.0.0 (2018-03-05)

Features

BREAKING CHANGES

  • expectPage() is replaced by expect(page)
  • Puppeteer launch options are now specified under launch object

1.1.1 (2018-03-04)

Bug Fixes

  • expect-puppeteer: add all sources in pkg (bb51870)

1.1.0 (2018-03-04)

Features

  • add jest-puppeteer-preset (7fbb273)
  • add spawnd & expect-puppeteer (6b7f5a4)