JSPM

  • Created
  • Published
  • Downloads 80618
  • Score
    100M100P100Q147207F
  • License Apache-2.0

Record, replay, and stub HTTP Interactions

Package Exports

  • @pollyjs/core

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@pollyjs/core) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Polly.JS

Record, Replay, and Stub HTTP Interactions

Build Status npm version license

Polly.JS is a standalone, framework-agnostic JavaScript library that enables recording, replaying, and stubbing HTTP interactions.

Polly taps into native browser APIs to mock requests and responses with little to no configuration while giving you the ability to take full control of each request with a simple, powerful, and intuitive API.

Why Polly?

Keeping fixtures and factories in parity with your APIs can be a time consuming process. Polly alleviates this by recording and maintaining actual server responses without foregoing flexibility.

  • Record your test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests.
  • Use Polly's client-side server to modify or intercept requests and responses to simulate different application states (e.g. loading, error, etc.).

Features

  • 🚀 Fetch & XHR Support
  • ⚡️️ Simple, Powerful, & Intuitive API
  • 💎 First Class Mocha & QUnit Test Helpers
  • 🔥 Intercept, Pass-Through, and Attach Events
  • 📼 Record to Disk or Local Storage
  • ⏱ Slow Down or Speed Up Time

Installation

Note that you must have node (and npm) installed.

npm install @pollyjs/core -D

If you want to install it with yarn:

yarn add @pollyjs/core -D

Getting Started

Check out the Quick Start documentation to get started.

Usage

Lets take a look at what an example test case would look like using Polly.

import { Polly } from '@pollyjs/core';

describe('Netflix Homepage', function() {
  it('should be able to sign in', async function() {
    /*
      Create a new polly instance.

      By default, Polly will connect to both fetch and XHR browser APIs and
      will record any requests that it hasn't yet seen while replaying ones it
      has already recorded.
    */
    const polly = new Polly('Sign In');
    const { server } = polly;

    /* Intercept all Google Analytic requests and respond with a 200 */
    server
      .get('/google-analytics/*path')
      .intercept((req, res) => res.sendStatus(200));

    /* Pass-through all GET requests to /coverage */
    server.get('/coverage').passthrough();

    /* start: pseudo test code */
    await visit('/login');
    await fillIn('email', 'polly@netflix.com');
    await fillIn('password', '@pollyjs');
    await submit();
    /* end: pseudo test code */

    expect(location.pathname).to.equal('/browse');

    /*
      Calling `stop` will persist requests as well as disconnect from any
      connected browser APIs (e.g. fetch or XHR).
    */
    await polly.stop();
  });
});

License

Copyright (c) 2018 Netflix, Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.