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

@scaleleap/jest-polly

v1.6.17

Published

Integrate Jest with PollyJS for a smooth experience of HTTP recording.

Downloads

4,529

Readme

@scaleleap/jest-polly

logo

Smoothest Jest integration with PollyJS.


Integrate Jest with PollyJS for a smooth HTTP recording and playback experience for your integration tests.

List of features

Environment Variables

Polly Mode

Can be set via POLLY_MODE environment variable.

Mode can be one of the following:

  • replay: Replay responses from recordings.
  • record: Force Polly to record all requests. This will overwrite recordings that already exist.
  • passthrough: Passes all requests through directly to the server without recording or replaying.

Default: replay

Usage:

POLLY_MODE=record npm t

Record if Missing

If a request's recording is not found, pass-through to the server and record the response.

Can be set via POLLY_RECORD_IF_MISSING environment variable.

Default: false if running in CI environment or true otherwise.

Usage:

POLLY_RECORD_IF_MISSING=true npm t

Secret Sanitization

Sometimes requests and/or responses may contain secret data, such as API keys, or oAuth tokens.

To automatically sanitize the recordings, you may add a list of secrets to the config to be replaced.

See "Change PollyJS default config" section for details.

// use a Record-style config, where keys are secrets,
// and values are what they will be replaced with
jestPollyConfigService.config = {
  secrets: {
    'somepassword': 'x',
    'my-api-key': 'x',
  }
}

// or simply use an array, and everything will be replaced with `x` by default:
jestPollyConfigService.config = {
  secrets: [process.env.MY_SECRET_VALYE]
}

Code Demo

Use in all tests

In your package.json

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

Or in jest.config.js

module.exports = {
  setupFilesAfterEnv: ['@scaleleap/jest-polly'],
};

Use in a single test

In my.test.js

import '@scaleleap/jest-polly';
import fetch from 'node-fetch';

test('is ok', async () => {
  const response = await fetch('https://httpstat.us/200');
  expect(response.ok).toBe(true);
});

Using the Polly instance

Use the polly instance to change default behavior.

import { jestPollyContext } from '@scaleleap/jest-polly';
import fetch from 'node-fetch';

jestPollyContext
  .polly
  .server
  .any('https://httpstat.us/500')
  .intercept((req, res) => res.sendStatus(500));

test('is not ok', async () => {
  const response = await fetch('https://httpstat.us/500');
  expect(response.ok).not.toBe(true);
});

Change PollyJS default config

If you want to change the default config, use the following setter.

Note: the config will be merged with the default config, and not overwritten.

import { jestPollyConfigService } from '@scaleleap/jest-polly';

jestPollyConfigService.config = {
  matchRequestsBy: {
    order: false
  }
}

Download & Installation

npm i -D @scaleleap/jest-polly

Contributing

Keep it simple. Keep it minimal. Don't put every single feature just because you can.

Authors or Acknowledgments

License

This project is licensed under the MIT License