Skip to content

Commit

Permalink
Properly invoke response interceptors
Browse files Browse the repository at this point in the history
  • Loading branch information
kingjan1999 committed Sep 1, 2022
1 parent a4db517 commit 42fd6fd
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ test
.idea/
.vscode/
coverage/
.tool-versions
tsconfig.json
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
# Changelog

## [4.7.0] - 2022-09-01

### Fixed

- Using `Promise.reject` in interceptors triggers `UnhandledPromiseRejection` (#87)

### Changed

- Calling `MockAxios.reset()` now resets interceptors as well

### Other

- Updated dependencies (Jest 28)

## [4.6.1] - 2022-04-13

### Changed
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ it("UppercaseProxy should get data from the server and convert it to UPPERCASE",

## Interceptors

AxiosMock offers basic support for interceptors (i.e. it does not break when interceptors are used in tested code). However, interceptors are not applied to the mocked requests / responses at the moment.
AxiosMock offers basic support for interceptors (i.e. it does not break when interceptors are used in tested code).

## Cancelling requests

Expand Down
2 changes: 2 additions & 0 deletions lib/mock-axios-types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {jest} from '@jest/globals';
import { SynchronousPromise, UnresolvedSynchronousPromise } from "synchronous-promise";

export interface HttpResponse {
Expand All @@ -11,6 +12,7 @@ export interface HttpResponse {
interface Interceptor {
use: jest.Mock<number, [onFulfilled?: (value: any) => any | Promise<any>, onRejected?: (error: any) => any]>;
eject: jest.Mock<void, [number]>;
clear: jest.Mock<void, []>;
}

interface Interceptors {
Expand Down
28 changes: 22 additions & 6 deletions lib/mock-axios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ MockAxios.interceptors = {
eject: jest.fn((position: number) => {
_requestInterceptors.splice(position - 1, 1);
}),
// @ts-ignore
clear: jest.fn(() => {
_requestInterceptors.length = 0;
}),
},
response: {
// @ts-ignore
Expand All @@ -123,6 +127,10 @@ MockAxios.interceptors = {
eject: jest.fn((position: number) => {
_responseInterceptors.splice(position - 1, 1);
}),
// @ts-ignore
clear: jest.fn(() => {
_responseInterceptors.length = 0;
}),
},
};

Expand Down Expand Up @@ -201,18 +209,20 @@ MockAxios.mockResponse = (
response,
);

const promise = popQueueItem(queueItem);
let promise = popQueueItem(queueItem);

if (!promise && !silentMode) {
throw new Error("No request to respond to!");
} else if (!promise) {
return;
}

const result = processInterceptors(response, _responseInterceptors, 'onFulfilled');
for (const interceptor of _responseInterceptors) {
promise = promise.then(interceptor.onFulfilled, interceptor.onRejected) as UnresolvedSynchronousPromise<any>;
}

// resolving the Promise with the given response data
promise.resolve(result);
promise.resolve(response);
};

MockAxios.mockResponseFor = (
Expand All @@ -239,7 +249,7 @@ MockAxios.mockError = (
queueItem: SynchronousPromise<any> | AxiosMockQueueItem = null,
silentMode: boolean = false,
) => {
const promise = popQueueItem(queueItem);
let promise = popQueueItem(queueItem);

if (!promise && !silentMode) {
throw new Error("No request to respond to!");
Expand All @@ -251,10 +261,12 @@ MockAxios.mockError = (
error.isAxiosError = true;
}

const result = processInterceptors(error, _responseInterceptors, 'onRejected');
for (const interceptor of _responseInterceptors) {
promise = promise.then(interceptor.onFulfilled, interceptor.onRejected) as UnresolvedSynchronousPromise<any>;;
}

// resolving the Promise with the given error
promise.reject(result);
promise.reject(error);
};

MockAxios.isAxiosError = (payload) => (typeof payload === 'object') && (payload.isAxiosError === true);
Expand Down Expand Up @@ -332,6 +344,10 @@ MockAxios.reset = () => {
MockAxios.options.mockClear();
MockAxios.request.mockClear();
MockAxios.all.mockClear();


MockAxios.interceptors.request.clear();
MockAxios.interceptors.response.clear();
};

MockAxios.Cancel = Cancel;
Expand Down

0 comments on commit 42fd6fd

Please sign in to comment.