-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
24.02.51: axios mock update, docs [WTEL-3802, WTEL-3803, WTEL-3804]
- Loading branch information
Showing
4 changed files
with
77 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# Axios Mock | ||
|
||
Mocking axios requests for testing purposes. | ||
|
||
```js | ||
// setup file, or actual test file | ||
import axiosMock from '@webitel/ui-sdk/src/tests/mocks/axiosMock'; | ||
|
||
// pass vitest instance | ||
const mock = axiosMock(); | ||
vi.doMock('axios', mock); | ||
``` | ||
|
||
Note: is you want to mock all requests, globally, do it in setup file for tests, | ||
like `tests/config/config.js` in consumer project. | ||
|
||
## What if you need to override some of the mocks? | ||
|
||
Make a deep merge of your custom mock with axiosMock instance. | ||
|
||
```js | ||
import axiosMock from '@webitel/ui-sdk/src/tests/mocks/axiosMock'; | ||
import merge from 'deepmerge'; | ||
|
||
const mock = axiosMock({ | ||
default: { | ||
request: jest.fn(), | ||
}, | ||
}); | ||
``` | ||
|
||
## vi.mock vs vi.doMock? | ||
|
||
### tldr; | ||
`.mock` is hoisted, so it won't access any other variables, | ||
`.doMock` is not hoisted, so it will access other variables. | ||
|
||
```js | ||
// For instance | ||
const mock = axiosMock(); | ||
vi.mock('axios', mock); // won't work | ||
vi.doMock('axios', mock); // will work | ||
|
||
// BUT | ||
vi.mock('axios', axiosMock()); // will work | ||
``` | ||
|
||
if you're not sure, use `doMock` | ||
|
||
### More info | ||
|
||
[github issue](https://github.com/vitest-dev/vitest/issues/4872) | ||
|
||
[doMock](https://vitest.dev/api/vi.html#vi-mock) isn't hoisted, | ||
but [mock](https://vitest.dev/api/vi.html#vi-domock) is |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,25 @@ | ||
/* | ||
Why .doMock() and not .mock()? | ||
import deepmerge from 'deepmerge'; | ||
|
||
https://github.com/vitest-dev/vitest/issues/4872 | ||
doMock isn't hoisted, but mock is | ||
https://vitest.dev/api/vi.html#vi-mock | ||
https://vitest.dev/api/vi.html#vi-domock | ||
*/ | ||
|
||
const axiosMock = (vi) => vi.doMock('axios', () => { | ||
return { | ||
default: { | ||
post: vi.fn(() => Promise.resolve({ data: {}})), | ||
get: vi.fn(() => Promise.resolve({ data: {}})), | ||
delete: vi.fn(() => Promise.resolve({ data: {}})), | ||
put: vi.fn(() => Promise.resolve({ data: {}})), | ||
patch: vi.fn(() => Promise.resolve({ data: {}})), | ||
request: vi.fn(() => Promise.resolve({ data: {}})), | ||
create: vi.fn().mockReturnThis(), | ||
interceptors: { | ||
request: { | ||
use: vi.fn(), eject: vi.fn(), | ||
}, response: { | ||
use: vi.fn(), eject: vi.fn(), | ||
const axiosMock = (customMock = {}) => () => deepmerge( | ||
{ | ||
default: { | ||
post: vi.fn(() => Promise.resolve({ data: {} })), | ||
get: vi.fn(() => Promise.resolve({ data: {} })), | ||
delete: vi.fn(() => Promise.resolve({ data: {} })), | ||
put: vi.fn(() => Promise.resolve({ data: {} })), | ||
patch: vi.fn(() => Promise.resolve({ data: {} })), | ||
request: vi.fn(() => Promise.resolve({ data: {} })), | ||
create: vi.fn().mockReturnThis(), | ||
interceptors: { | ||
request: { | ||
use: vi.fn(), eject: vi.fn(), | ||
}, response: { | ||
use: vi.fn(), eject: vi.fn(), | ||
}, | ||
}, | ||
}, | ||
}, | ||
}; | ||
}); | ||
customMock, | ||
); | ||
|
||
export default axiosMock; |