Security News
Introducing the Socket Python SDK
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
jest-environment-puppeteer
Advanced tools
The jest-environment-puppeteer package provides a Jest test environment that uses Puppeteer, a headless Chrome Node API, to run browser tests. This allows you to write tests that interact with a real browser, making it ideal for end-to-end (E2E) testing, UI testing, and web scraping.
End-to-End Testing
This feature allows you to perform end-to-end testing by interacting with a real browser. The code sample demonstrates how to navigate to Google's homepage and check if the text 'google' is present on the page.
const puppeteer = require('puppeteer');
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');
});
});
UI Testing
This feature allows you to perform UI testing by simulating user interactions. The code sample demonstrates how to click a button on a webpage and verify that a message is displayed as a result.
const puppeteer = require('puppeteer');
describe('Button Click', () => {
beforeAll(async () => {
await page.goto('https://example.com');
});
it('should click a button and display a message', async () => {
await page.click('#myButton');
const message = await page.$eval('#message', el => el.textContent);
expect(message).toBe('Button clicked!');
});
});
Web Scraping
This feature allows you to perform web scraping by extracting information from web pages. The code sample demonstrates how to navigate to a webpage and scrape the title of the page.
const puppeteer = require('puppeteer');
describe('Web Scraping', () => {
beforeAll(async () => {
await page.goto('https://example.com');
});
it('should scrape the title of the page', async () => {
const title = await page.title();
expect(title).toBe('Example Domain');
});
});
jest-puppeteer provides a similar environment for running Puppeteer tests with Jest. It offers a more integrated setup with additional utilities and configurations specifically designed for Puppeteer, making it easier to write and manage browser tests.
Puppeteer is a Node library that provides a high-level API to control Chrome or Chromium over the DevTools Protocol. While it doesn't integrate directly with Jest, it can be used in conjunction with Jest to perform browser automation tasks. jest-environment-puppeteer essentially builds on top of Puppeteer to provide a seamless Jest environment.
Cypress is an end-to-end testing framework that provides a complete solution for running browser tests. Unlike jest-environment-puppeteer, Cypress comes with its own test runner and assertion library, offering a more comprehensive and user-friendly experience for writing and running browser tests.
Run your tests using Jest & Puppeteer 🎪✨
npm install jest-environment-puppeteer
Update your Jest configuration:
{
"globalSetup": "jest-environment-puppeteer/globalSetup",
"globalTeardown": "jest-environment-puppeteer/globalTeardown",
"testEnvironment": "jest-environment-puppeteer/testEnvironment"
}
Use Puppeteer in your tests:
describe('Google', () => {
beforeAll(async () => {
await mainPage.goto('https://google.com')
})
it('should display "google" text on page', async () => {
const text = await mainPage.evaluate(() => document.body.textContent)
expect(text).toContain('google')
})
})
To write your integration tests using Puppeteer you can find all available methods in Puppeteer documentation.
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 = {
dumpio: true,
headless: process.env.HEADLESS !== 'false',
}
Sometimes you want to use your own environment, to do that you can extend PuppeteerEnvironment
.
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
globalSetup
or globalTeardown
It is possible to access globalSetup
or globalTeardown
in your scripts.
import { globalSetup, globalTeardown } from 'jest-environment-puppeteer'
async function setup() {
await globalSetup()
// ...
}
async function teardown() {
// ...
await globalTeardown()
}
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.mainPage
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')
})
Thanks to Fumihiro Xue for his great Jest example.
MIT
FAQs
Puppeteer environment for Jest.
The npm package jest-environment-puppeteer receives a total of 157,071 weekly downloads. As such, jest-environment-puppeteer popularity was classified as popular.
We found that jest-environment-puppeteer demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
Security News
Floating dependency ranges in npm can introduce instability and security risks into your project by allowing unverified or incompatible versions to be installed automatically, leading to unpredictable behavior and potential conflicts.
Security News
A new Rust RFC proposes "Trusted Publishing" for Crates.io, introducing short-lived access tokens via OIDC to improve security and reduce risks associated with long-lived API tokens.