GithubHelp home page GithubHelp logo

callumlocke / jest-puppeteer Goto Github PK

View Code? Open in Web Editor NEW

This project forked from argos-ci/jest-puppeteer

1.0 2.0 0.0 187 KB

Run your tests using Jest & Puppeteer ๐ŸŽชโœจ

JavaScript 97.78% HTML 2.22%

jest-puppeteer's Introduction

Jest Puppeteer

Build Status version MIT License

Run your tests using Jest & Puppeteer ๐ŸŽชโœจ

npm install --save-dev jest-puppeteer puppeteer jest

Requires Jest v22+ TypeScript users should additionally install @types/puppeteer, @types/jest-environment-puppeteer and @types/expect-puppeteer

Usage

Update your Jest configuration:

{
  "preset": "jest-puppeteer"
}

Use Puppeteer in your tests:

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

  it('should display "google" text on page', async () => {
    await expect(page).toMatch('google')
  })
})

Recipes

Writing tests using Puppeteer

Writing integration test can be done using Puppeteer API but it can be complicated and hard because API is not designed for testing.

To make it simpler, expect-puppeteer API add some specific matchers if you make expectation on a Puppeteer Page.

Some examples:

Find a text in the page

// Assert that current page contains 'Text in the page'
await expect(page).toMatch('Text in the page')

Click a button

// Assert that a button containing text "Home" will be clicked
await expect(page).toClick('button', { text: 'Home' })

Fill a form

// Assert that a form will be filled
await expect(page).toFillForm('form[name="myForm"]', {
  firstName: 'James',
  lastName: 'Bond',
})

Start a server

Jest Puppeteer integrates a functionality to start a server when running your test suite. It automatically closes the server when tests are done.

To use it, specify a server section in your jest-puppeteer.config.js.

// jest-puppeteer.config.js
module.exports = {
  server: {
    command: 'node server.js',
    port: 4444,
  },
}

Other options are documented in Jest Environment Puppeteer readme.

Configure Puppeteer

Jest Puppeteer automatically detect the best config to start Puppeteer but sometimes you may need to specify custom options. All Puppeteer launch options can be specified in jest-puppeteer.config.js at the root of the project. Since it is JavaScript, you can use all stuff you need, including environment.

// jest-puppeteer.config.js
module.exports = {
  launch: {
    dumpio: true,
    headless: process.env.HEADLESS !== 'false',
  },
}

Configure ESLint

Jest Puppeteer exposes two new globals: browser, page. If you want to avoid errors, you can add them to your .eslintrc.js:

// .eslintrc.js
module.exports = {
  env: {
    jest: true,
  },
  globals: {
    page: true,
    browser: true,
  },
}

Extend PuppeteerEnvironment

Sometimes you want to use your own environment, to do that you can extend PuppeteerEnvironment.

First, create your own js file for custom environment.

// custom-environment.js
const PuppeteerEnvironment = require('jest-environment-puppeteer')

class CustomEnvironment extends PuppeteerEnvironment {
  async setup() {
    await super.setup()
    // Your setup
  }

  async teardown() {
    // Your teardown
    await super.teardown()
  }
}

module.exports = CustomEnvironment

Then, assigning your js file path to the testEnvironment property in your Jest configuration.

{
  // ...
  "testEnvironment": "./custom-environment.js"
}

Now your custom setup and teardown will be triggered before and after each test suites.

Create your own globalSetup and globalTeardown

It is possible to create your own globalSetup and globalTeardown.

For this use case, jest-environment-puppeteer exposes two methods: setup and teardown, so that you can wrap them with your own global setup and global teardown methods as the following example:

// global-setup.js
const { setup: setupPuppeteer } = require('jest-environment-puppeteer')

module.exports = async function globalSetup() {
  await setupPuppeteer()
  // Your global setup
}
// global-teardown.js
const { teardown: teardownPuppeteer } = require('jest-environment-puppeteer')

module.exports = async function globalTeardown() {
  // Your global teardown
  await teardownPuppeteer()
}

Then assigning your js file paths to the globalSetup and globalTeardown property in your Jest configuration.

{
  // ...
  "globalSetup": "./global-setup.js",
  "globalTeardown": "./global-teardown.js"
}

Now your custom globalSetup and globalTeardown will be triggered once before and after all test suites.

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.expect(page)

Helper to make Puppeteer assertions, see documentation.

await expect(page).toMatch('A text in the page')
// ...

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.

  • launch <object> All Puppeteer launch options can be specified in config. Since it is JavaScript, you can use all stuff you need, including environment.
  • server <Object> Server options
    • command <string> Command to start server
    • port <number> If specified, it will wait port to be listened
    • options <Object> Optional options for spawnd
// jest-puppeteer.config.js
module.exports = {
  launch: {
    dumpio: true,
    headless: process.env.HEADLESS !== 'false',
  },
  server: {
    command: 'node server.js',
    port: 4444,
  },
}

Inspiration

Thanks to Fumihiro Xue for his great Jest example.

License

MIT

jest-puppeteer's People

Contributors

creativeprojects avatar detrohutt avatar gregberge avatar kaufmann42 avatar melissachang avatar ntwb avatar rkoval avatar rzane avatar testerez avatar tkrotoff avatar trysound avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.