Skip to content
Record, Replay, and Stub HTTP Interactions.
JavaScript Other
Branch: master
Clone or download
agraves and jasonmit fix: use watch strategy (#236)
* fix: Implement watch strategy for tests & development

* in one tab, run `yarn watch` and wait for it to settle
* run `yarn test` in another tab
* watch should correctly watch all of the packages and tests and
  re-run as needed

* fix: Review Fixes

* NB: aliased test:ci to test
* NB: moved testem runner to test:watch
* remove invalid watch spec for rollup files
* remove specialized watch commands since watch covers everything now
* added a script to ensure server, app and tests are built before running testem
* updated persister-in-memory to support new (required) yarn commands
Latest commit 5b4edf3 Aug 11, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github chore: Create github issue and pull request templates Feb 22, 2019
docs feat: Implement `expiryStrategy` (#227) Jul 17, 2019
examples ci: Fixes linting error for demo prop types (#225) Jul 5, 2019
packages/@pollyjs fix: use watch strategy (#236) Aug 11, 2019
scripts fix: use watch strategy (#236) Aug 11, 2019
tests feat: Adds an in-memory persister to test polly internals (#237) Aug 1, 2019
.eslintignore feat: Lint other filetypes with prettier (#152) Jan 8, 2019
.eslintrc.js chore: Remove class properties babel plugin and babel-eslint (#134) Nov 11, 2018
.gitignore ci: Removes mock-fs and fixes CI on Node 10 LTS (#151) Dec 29, 2018
.prettierrc.json feat: Lint other filetypes with prettier (#152) Jan 8, 2019
.travis.yml chore: Remove `sudo: false` usage from .travis.yml (#201) Apr 18, 2019
CHANGELOG.md chore: Publish v2.6.2 Aug 5, 2019
CONTRIBUTING.md fix: use watch strategy (#236) Aug 11, 2019
LICENSE chore: Initial commit. Jun 1, 2018
OSSMETADATA chore: Initial commit. Jun 1, 2018
README.md chore: Adds contributor section to README (#235) Jul 18, 2019
commitlint.config.js revert: Revert "Update commitlint.config.js" Jun 8, 2018
jest.config.js test: Fix hanging jest test case (#130) Nov 9, 2018
lerna.json chore: Publish v2.6.2 Aug 5, 2019
package.json fix: use watch strategy (#236) Aug 11, 2019
testem.js fix: use watch strategy (#236) Aug 11, 2019
yarn.lock chore: Lodash security update, yarn dedupe (#239) Jul 21, 2019

README.md

Polly.JS

Record, Replay, and Stub HTTP Interactions

Build Status license

Polly.JS is a standalone, framework-agnostic JavaScript library that enables recording, replaying, and stubbing of HTTP interactions. By tapping into multiple request APIs across both Node & the browser, Polly.JS is able 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.

Interested in contributing or just seeing Polly in action? Head over to CONTRIBUTING.md to learn how to spin up the project!

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

  • 🚀 Node & Browser 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

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';
import XHRAdapter from '@pollyjs/adapter-xhr';
import FetchAdapter from '@pollyjs/adapter-fetch';
import RESTPersister from '@pollyjs/persister-rest';

/*
  Register the adapters and persisters we want to use. This way all future
  polly instances can access them by name.
*/
Polly.register(XHRAdapter);
Polly.register(FetchAdapter);
Polly.register(RESTPersister);

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

      Connect Polly to both fetch and XHR browser APIs. By default, it will
      record any requests that it hasn't yet seen while replaying ones it
      has already recorded.
    */
    const polly = new Polly('Sign In', {
      adapters: ['xhr', 'fetch'],
      persister: 'rest'
    });
    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();
  });
});

The above test case would generate the following HAR file which Polly will use to replay the sign-in response when the test is rerun:

{
  "log": {
    "_recordingName": "Sign In",
    "browser": {
      "name": "Chrome",
      "version": "67.0"
    },
    "creator": {
      "name": "Polly.JS",
      "version": "0.5.0",
      "comment": "persister:rest"
    },
    "entries": [
      {
        "_id": "06f06e6d125cbb80896c41786f9a696a",
        "_order": 0,
        "cache": {},
        "request": {
          "bodySize": 51,
          "cookies": [],
          "headers": [
            {
              "name": "content-type",
              "value": "application/json; charset=utf-8"
            }
          ],
          "headersSize": 97,
          "httpVersion": "HTTP/1.1",
          "method": "POST",
          "postData": {
            "mimeType": "application/json; charset=utf-8",
            "text": "{\"email\":\"polly@netflix.com\",\"password\":\"@pollyjs\"}"
          },
          "queryString": [],
          "url": "https://netflix.com/api/v1/login"
        },
        "response": {
          "bodySize": 0,
          "content": {
            "mimeType": "text/plain; charset=utf-8",
            "size": 0
          },
          "cookies": [],
          "headers": [],
          "headersSize": 0,
          "httpVersion": "HTTP/1.1",
          "redirectURL": "",
          "status": 200,
          "statusText": "OK"
        },
        "startedDateTime": "2018-06-29T17:31:55.348Z",
        "time": 11,
        "timings": {
          "blocked": -1,
          "connect": -1,
          "dns": -1,
          "receive": 0,
          "send": 0,
          "ssl": -1,
          "wait": 11
        }
      }
    ],
    "pages": [],
    "version": "1.2"
  }
}

Credits

In alphabetical order:

Contributors

We're hiring!

Join the Netflix Studio UI team to help us build projects like this! You can check out our open roles here on our team page.

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.

You can’t perform that action at this time.