Sit back, relax, and enjoy automatic mocking for axios HTTP requests
yarn add axios
yarn add axios-record-replay-adapter --devnpm i axios --save
npm i axios-record-replay-adapter --save-devWhen running tests in watch mode, the recordings directory must be ignored to prevent recording files from triggering tests to re-run.
{
"jest": {
"watchPathIgnorePatterns": [
"<rootDir>/recordings"
]
}
}useAxiosRecordReplayAdapter()const customAxiosIntance = axios.create()
useAxiosRecordReplayAdapter({
axiosInstance: customAxiosInstance
recordingsDir: './tests/recordings'
})axios-record-replay-adpater returns a function to restore the default axios adapter.
const restoreDefaultAdapater = useAxiosRecordReplay()
restoreDefaultAdapater()useAxiosRecordReplayAdapter({
buildRequest(axiosRequestConfig) {
return {
path: new URL(axiosRequestConfig.url).pathname
}
}
})useAxiosRecordReplayAdapter({
buildResponse(axiosResponse) {
return {
data: axiosResponse.data
}
}
})NOTE: The result of
buildRequest()gets passed tobuildFilenamePrefix(request)
useAxiosRecordReplayAdapter({
buildFilenamePrefix(requestFromBuildRequest) {
return request.path.replace(/\//g, '-').slice(1)
}
})- Fork this repository to your own GitHub account and then clone it to your local device
- Install the dependecies using
yarn - Link the package to the global module directory:
yarn link - Run
yarn test --watchand start making your changes - You can use
yarn link axios-record-replay-adapterto test your changes in a local project - Ensure any changes are documented in
CHANGELOG.md
MIT © Brent Mealhouse