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

@spotify/polly-jest-presets

v3.1.1

Published

Presets for Jest that make Polly.js plug-and-play

Downloads

54

Readme

polly-jest-presets

Actions Status Version

An opinionated configuration and wrapper around Polly and setup-polly-jest to have automatic recording and playback of network requests made during your Jest tests.

Note: Polly Jest Presets bundles in all necessary Polly packages to make the setup as easy as possible for a typical Node-based web app. It uses File persister to store recordings locally. Therefore, there's no need to install Polly or Polly Jest bindings separately in your project after including this preset.

Packages included:

"@pollyjs/adapter-node-http": "^2.6.0",
"@pollyjs/core": "^2.6.0",
"@pollyjs/persister-fs": "^2.6.0",
"setup-polly-jest": "^0.5.2",

Usage

Install the preset as a dev dependency:

yarn add @spotify/polly-jest-presets -D

Add the preset to your Jest config (by default jest.config.js), in the setupFilesAfterEnv:

{
  "setupFilesAfterEnv": ["@spotify/polly-jest-presets"]
}

Or import in an individual test.

// ./my.test.js
import '@spotify/polly-jest-presets';

Getting Started

To test it out, make a network request in one of your tests.

import '@spotify/polly-jest-presets';
// `yarn add -D node-fetch` for this demo
import fetch from 'node-fetch';

describe('a dummy test', () => {
  it('fetches something', async () => {
    const resp = await fetch('https://reqres.in/api/users?page=2');
    const payload = await resp.json();
    expect(payload.data.length).toBeGreaterThan(1);
  });
});

First, you need to run the tests with the POLLY_MODE environment variable set to record. This will tell Polly that you intend for all of the requests to record in this test run.

POLLY_MODE="record" jest

You should now see a __recordings__ directory next to your test file. It should contain a .har file which shows the request we made within the it block.

To test that playback works, disconnect your internet on your machine and run:

POLLY_MODE="replay" jest

The test still passes! Note: the default POLLY_MODE is replay.

Configuration and API usage

If you want to override Polly configuration, you can add configuration to globals.pollyConfig in the Jest config:

{
  "globals": {
    "pollyConfig": {
      "expiresIn": "3 months"
    }
  }
}

See all of the valid Polly options in the Polly documentation.

You may also want to get at the global Polly instance. You can import it:

// if you need access to the pollyContext
import { pollyContext } from '@spotify/polly-jest-presets';

pollyContext.polly.server
  .get('/series')
  .intercept((req, res) => res.sendStatus(200));

See all of the Polly API methods in the Polly documentation.

Opinions

This preset has a few opinions baked in. All of these are overridable by setting the globals.pollyConfig in your Jest config.

Expire recordings often

We think it's safer to expire recordings frequently. The default expiresIn is set to "14d" in this preset. By default, expiryStrategy is configured to warn.

Explicit recording only (no recordIf*)

We think it makes more sense to avoid recording in the background for test authoring to avoid unexpected changes to checked in .har files. Because of this, we have set all of the recordIf* config values to false. This means that your tests will fail when you first write them if you don't override POLLY_MODE.

Contributing

See CONTRIBUTING guidelines.