Code Monkey home page Code Monkey logo

axios-record-replay-adapter's Introduction

axios-record-replay-adapter ๐ŸŽฅ ๐ŸŽฌ ๐Ÿฟ

npm version npm downloads styled with prettier

Sit back, relax, and enjoy automatic mocking for axios HTTP requests

Table of contents

Installation

yarn

yarn add axios
yarn add axios-record-replay-adapter --dev

npm

npm i axios --save
npm i axios-record-replay-adapter --save-dev

Setup

Ignore recordings directory

When running tests in watch mode, the recordings directory must be ignored to prevent recording files from triggering tests to re-run.

Jest

{
  "jest": {
    "watchPathIgnorePatterns": [
      "<rootDir>/recordings"
    ]
  }
}

Usage

With defaults

useAxiosRecordReplayAdapter()

With options

const customAxiosIntance = axios.create()
useAxiosRecordReplayAdapter({
  axiosInstance: customAxiosInstance
  recordingsDir: './tests/recordings'
})

Restore axios default adapter

axios-record-replay-adpater returns a function to restore the default axios adapter.

const restoreDefaultAdapater = useAxiosRecordReplay()
restoreDefaultAdapater()

Advanced usage

With buildRequest()

useAxiosRecordReplayAdapter({
  buildRequest(axiosRequestConfig) {
    return {
      path: new URL(axiosRequestConfig.url).pathname
    }
  }
})

With buildResponse()

useAxiosRecordReplayAdapter({
  buildResponse(axiosResponse) {
    return {
      data: axiosResponse.data
    }
  }
})

With buildFilenamePrefix()

NOTE: The result of buildRequest() gets passed to buildFilenamePrefix(request)

useAxiosRecordReplayAdapter({
  buildFilenamePrefix(requestFromBuildRequest) {
    return request.path.replace(/\//g, '-').slice(1)
  }
})

Contributing

  1. Fork this repository to your own GitHub account and then clone it to your local device
  2. Install the dependecies using yarn
  3. Link the package to the global module directory: yarn link
  4. Run yarn test --watch and start making your changes
  5. You can use yarn link axios-record-replay-adapter to test your changes in a local project
  6. Ensure any changes are documented in CHANGELOG.md

License

MIT ยฉ Brent Mealhouse

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.