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

jest-page-tester

v2.0.0

Published

Extends Jest with functionality for loading real pages into jsdom.

Downloads

7,804

Readme

Jest Page Tester

npm version

Extends Jest with functionality for loading real pages into jsdom.

Intended for cases where we want to run integration tests against real pages without dealing with all the implicit flakiness of running those tests in real browsers.

Installation

# npm
npm install jest-page-tester -D

# yarn
yarn add jest-page-tester -D

Setup

Add the jest-page-tester preset to your Jest config, as follows:

// jest.config.js
module.exports = {
  testEnvironment: 'jest-page-tester',
}

Usage

The following functions will be made available to your tests via the global page object.

page.loadPage()

Load a page into the DOM.

it('renders the page title', async () => {
  await page.loadPage('/my/page');

  const title = document.querySelector('h1');

  expect(title).toBe('My Page');
});

page.loadScripts()

Load external scripts (which will not be loaded by default).

it('runs the external script', async () => {
  await page.loadPage('/my/page');
  await page.loadScripts();

  // ...
});

Configuration

Jest page tester can be configured by adding a jest-page-tester.config.js file to the root of your repo, or by adding a jest-page-tester property to your package.json file.

The available options are documented below.

testURL

The base URL against which to run the tests (will be overwritten by the --testURL CLI arg).

// jest-page-tester.config.js
module.exports = {
  testURL: 'http://example.com',
}

block

A list regular expressions used to block the loading of external resources.

// jest-page-tester.config.js
const fetch = require('node-fetch');

module.exports = {
  block: [
    'www.googletagmanager.com',
    'ads.com',
  ],
};

fetch

Override the default fetch function used for requesting resources. Useful if we want to purge an edge cache each time we load a page, for example.

// jest-page-tester.config.js
const fetch = require('node-fetch');

module.exports = {
  fetch: async (url, opts) => {
    await fetch(url, { method: 'PURGE' });

    return fetch(url, opts);
  },
};

externalLogLevel

The level of messages to log when evaluating external resources (e.g. scripts). The default is to log no messages.

// jest-page-tester.config.js
module.exports = {
  externalLogLevel: 'error',
};

Cookies

Any cookies present in the document will be sent along with the request to load the page, for example:

it('sends cookies with the page request', () => {
  document.cookie = 'token=abc123;';

  await page.loadPage('/page');

  const signInThingy = document.querySelector('#signed-in');

  expect(signInThingy.textContent).toBe('Signed in cookie existed');
});

Similarly, if the response received when loading a page includes any Set-Cookie headers these will be loaded into the local DOM.

ESLint

Configure ESLint by adding the page global, as follows:

{
  "globals": {
    "page": "readonly"
  },
}