npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

jsdom-screenshot-playwright

v1.1.0

Published

Generate screenshots of jsdom using Playwright

Downloads

12

Readme

Publish

JSDom Screenshot Playwright

📸 Take screenshots of jsdom with playwright

Inspired by jsdom-screenshot

Table of Contents

Installation

yarn add -D jsdom-screenshot-playwright

Usage in Jest, React & react-testing-library

It is recommended to use this package with jest-image-snapshot and react-testing-library. Use it as together like this:

import { generateImage } from "jsdom-screenshot-playwright";
import { render } from "react-testing-library";
import { SomeComponent } from "<your-code>";

it("should have no visual regressions", async () => {
  render(<SomeComponent />);
  expect(await generateImage()).toMatchImageSnapshot();
});

Example of setupFilesAfterEnv in jest config:

// react-testing-library setup
import '@testing-library/jest-dom'
// playwright polyfills
import 'core-js'
// jest-image-snapshot setup
import { configureToMatchImageSnapshot } from 'jest-image-snapshot'
// jsdom-screenshot-playwright setup
import { close, start } from 'jsdom-screenshot-playwright'

// jest-image-snapshot setup
const toMatchImageSnapshot = configureToMatchImageSnapshot({
  diffDirection: 'vertical',
  // useful on CI (no need to retrieve the diff image, copy/paste image content from logs)
  dumpDiffToConsole: true,
  // use SSIM to limit false positive
  // https://github.com/americanexpress/jest-image-snapshot#recommendations-when-using-ssim-comparison
  comparisonMethod: 'ssim',
  customDiffConfig: {
    ssim: 'fast',
  },
  failureThreshold: 0.01,
  failureThresholdType: 'percent',
})
expect.extend({ toMatchImageSnapshot })

// jsdom-screenshot-playwright setup
beforeAll(async () => {
  // start jsdom-screenshot-playwright before all tests (to avoid starting it for each test and improve performance)
  await start(
    {
      defaultSelector: 'div', // first div element in rendered html
    },
    {
      // playwright context options
      viewport: {
        width: 800,
        height: 600,
      },
    }
  )
})

afterAll(async () => {
  // close jsdom-screenshot-playwright after all tests (close playwright instance)
  await close()
})

API

generateImage(options?: ScreenshotParams): Promise<Buffer>

Takes a screenshot of the current jsdom document and returns a Buffer containing the image data.

options

  • options.selector (optional) - A selector to use for the screenshot.
  • options.screenshotOptions (optional) - Options to pass to playwright's screenshot method.
  • options.contextOptions (optional) - Options to pass to playwright's launch context method.

start(options?: BrowserOptions, contextOptions?: BrowserContextOptions): Promise<void>

Starts a new playwright instance. This is useful if you want to use the same instance for multiple screenshots.

options

  • options.device (optional) - A device to emulate. Defaults to Desktop Firefox HiDPI.
  • options.launchOptions (optional) - Options to pass to playwright's launch options method.
  • options.defaultSelector (optional) - A selector to use for the screenshot. Defaults to body.
  • options.maxOpenPages (optional) - Maximum number of pages to open. Defaults to 10. (to increase performance)
  • options.debug (optional) - Enable debug mode (logs for internal actions). Defaults to false.

contextOptions

  • contextOptions (optional) - Options to pass to playwright's launch context method.

close(): Promise<void>

Closes the current playwright instance.

Attribution

This package is based on jsdom-screenshot by dferber90.