Détail du package

jest-playback

ikatyang26.6kMIT4.1.0

Record and playback HTTP requests from your Jest tests

jest, playback, record, request

readme

jest-playback

npm build

Record and playback HTTP requests from your Jest tests

Changelog

Install

npm install jest-playback

Usage

import * as jestPlayback from 'jest-playback'

await jestPlayback.setup()

test('example', async () => {
  const response = await fetch('http://www.example.com/')
  expect(response.status).toBe(200)
})

The records are stored as snapshots.

API

export default setup
export declare function setup(options?: Options): Promise<void>

export interface Options {
  /** @default Mode.Auto */
  mode?: Mode
  getRequestCacheKey?: (request: Request) => string | Promise<string>
}

export enum Mode {
  /**
   * - `Mode.Update` if Jest `--update-snapshot` flag specified
   * - `Mode.Play` if Jest `--ci` flag specified
   * - `Mode.Record` otherwise
   */
  Auto = 'auto',
  /**
   * - all requests are recorded
   */
  Update = 'update',
  /**
   * - play records
   * - new requests are blocked
   */
  Play = 'play',
  /**
   * - play records
   * - new requests are recorded
   */
  Record = 'record',
}

Development

# lint
pnpm run lint

# build
pnpm run build

# test with jest
pnpm run test:jest

# test with vitest
pnpm run test:vitest

License

MIT © Ika

changelog

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

4.1.0 (2023-07-25)

Features

4.0.0 (2023-07-22)

Features

  • rewrite with @mswjs/interceptors (#432) (cb66cf5)
    • use snapshot to store records
    • store records by test by request
    • support custom request cache key
    • compatible with Vitest

BREAKING CHANGES

  • this package is now pure ESM
  • this package now requires Node 18+
  • the record format has been changed
  • Mode has been replaced by the Jest --ci/--update-snapshot flag

3.0.0 (2020-07-29)

Features

BREAKING CHANGES

  • require Node 10+

2.0.2 (2018-11-30)

Bug Fixes

  • compute hash correctly for records with JSON body (#351) (e9c156a)

2.0.1 (2018-09-21)

Bug Fixes

2.0.0 (2018-07-28)

Features

BREAKING CHANGES

  • directly use require to setup instead of using reporter
  • drop options
    • playbacks and mode_env are not configurable
    • completely dropped debug
    • moved mode to the setup API

1.0.1 (2017-09-05)

Bug Fixes

v1.0.0 (2017-07-13)

🚀 New Feature

  • Release first version