Record and playback http requests from your Jest tests
TypeScript JavaScript
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.

README.md

jest-playback

npm build

Record and playback http requests from your Jest tests

Changelog

Install

# using npm
npm install --save-dev jest jest-playback

# using yarn
yarn add --dev jest jest-playback

NOTE: This project uses the reporters field, which is available since Jest v20.

Usage

Modify your Jest config so that looks something like:

(./package.json)

{
  "scripts": {
    "test": "jest"
  },
  "jest": {
    "reporters": [
      "default",
      "jest-playback"
    ]
  }
}

If you just want to record requests:

JEST_PLAYBACK_MODE=record npm run test

If you just want to playback records:

JEST_PLAYBACK_MODE=play npm run test

NOTE The default mode is run, which will playback records and still allow unmocked requests.

Modes

  • run

    • play records
    • enable net connet
  • play

    • play records
    • disable net connet
  • record

    • enable net connect
    • record all requests
  • real

    • enable net connect

Configs

configs are in the reporter's second field:

{
  "jest": {
    "reporters": [
      "default",
      ["jest-playback", {"option": "value"}]
    ]
  }
}
  • debug

    • type: boolean
    • default: false
    • display debug message
  • playbacks

    • type: string
    • default: <rootDir>/playbacks
    • specify directory to store records
  • mode

    • type: string
    • default: run
    • specify which mode to use
  • mode_env

    • type: string
    • default: JEST_PLAYBACK_MODE
    • specify what environment variable to be used as mode, has higher priority than mode

Development

# lint
yarn run lint

# build
yarn run build

# test
yarn run test

Related

  • nock: HTTP mocking and expectations library
  • ava-playback: Record and playback http requests from your ava tests

License

MIT © Ika