Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: decouple from cross-fetch #24

Merged
merged 11 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ module.exports = {
parser: '@typescript-eslint/parser',
parserOptions: {
tsconfigRootDir: __dirname,
project: './tsconfig.eslint.json',
// Because we are setting "type": "module" in our package.json, all `.js` files are treated as modules
// Sometimes we will want a commonjs file, like this eslint config, in which case we use the .cjs extension
extraFileExtensions: ['.cjs'],
project: './tsconfig.json',
},
plugins: ['@typescript-eslint'],
rules: {
Expand Down
5 changes: 3 additions & 2 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ jobs:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm run lint
- run: npm run tsc
- run: npm test
- run: npm run format:check
- run: npm run build
- run: npm run test
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ npm-debug.log
yarn-error.log
coverage
.idea
dist
46 changes: 20 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ requests. It's easy to setup and you don't need a library like `nock` to get goi
for mocking under the surface. This means that any of the `vi.fn()` methods are also available. For more information on
the vitest mock API, check their docs [here](https://vitest.dev/guide/mocking.html)

It currently supports the mocking with the [`cross-fetch`](https://www.npmjs.com/package/cross-fetch) polyfill, so it
supports Node.js and any browser-like runtime.
dirkluijk marked this conversation as resolved.
Show resolved Hide resolved
As of version 0.4.0, `vitest-fetch-mock` mocks the global [Fetch](https://developer.mozilla.org/en-US/docs/Web/API/Window/fetch) method,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@IanVS I assume the next version to be 0.4.0 here. Let me know if you have different ideas.

which should be present in all modern runtimes and browsers.

## Contents

Expand Down Expand Up @@ -631,8 +631,6 @@ the most used ones.
```js
// api.js

import 'cross-fetch';

export function APIRequest(who) {
if (who === 'facebook') {
return fetch('https://facebook.com');
Expand Down Expand Up @@ -749,16 +747,14 @@ For convenience, all the conditional mocking functions also accept optional para
#### Conditional Mocking examples

```js
import crossFetch from 'cross-fetch';

vi.mock('cross-fetch', async () => {
const crossFetchActual = await requireActual('cross-fetch');
vi.mock('fetch', async () => {
const fetchActual = globalThis.fetch;
const mocked = {};
for (const key of Object.keys(crossFetchActual)) {
if (typeof crossFetchActual[key] === 'function') {
mocked[key] = vi.fn(crossFetchActual[key]);
for (const key of Object.keys(fetchActual)) {
if (typeof fetchActual[key] === 'function') {
mocked[key] = vi.fn(fetchActual[key]);
} else {
mocked[key] = crossFetchActual[key];
mocked[key] = fetchActual[key];
}
}
return mocked;
Expand All @@ -768,18 +764,18 @@ describe('conditional mocking', () => {
const realResponse = 'REAL FETCH RESPONSE'
const mockedDefaultResponse = 'MOCKED DEFAULT RESPONSE'
const testUrl = defaultRequestUri
let crossFetchSpy
let fetchSpy
beforeEach(() => {
fetch.resetMocks()
fetch.mockResponse(mockedDefaultResponse)
vi.mocked(crossFetch)
vi.mocked(fetch)
.mockImplementation(async () =>
Promise.resolve(new Response(realResponse))
)
})

afterEach(() => {
vi.mocked(crossFetch).mockRestore()
vi.mocked(fetch).mockRestore()
})

const expectMocked = async (uri, response = mockedDefaultResponse) => {
Expand Down Expand Up @@ -981,16 +977,14 @@ describe('conditional mocking', () => {
```

```js
import crossFetch from 'cross-fetch';

vi.mock('cross-fetch', async () => {
const crossFetchActual = await requireActual('cross-fetch');
vi.mock('fetch', async () => {
const fetchActual = globalThis.fetch;
const mocked = {};
for (const key of Object.keys(crossFetchActual)) {
if (typeof crossFetchActual[key] === 'function') {
mocked[key] = vi.fn(crossFetchActual[key]);
for (const key of Object.keys(fetchActual)) {
if (typeof fetchActual[key] === 'function') {
mocked[key] = vi.fn(fetchActual[key]);
} else {
mocked[key] = crossFetchActual[key];
mocked[key] = fetchActual[key];
}
}
return mocked;
Expand All @@ -1007,15 +1001,15 @@ describe('conditional mocking complex', () => {
const realResponse = 'REAL FETCH RESPONSE';
const mockedDefaultResponse = 'MOCKED DEFAULT RESPONSE';
const testUrl = defaultRequestUri;
let crossFetchSpy;
let fetchSpy;
beforeEach(() => {
fetch.resetMocks();
fetch.mockResponse(mockedDefaultResponse);
vi.mocked(crossFetch).mockImplementation(async () => Promise.resolve(new Response(realResponse)));
vi.mocked(fetch).mockImplementation(async () => Promise.resolve(new Response(realResponse)));
});

afterEach(() => {
vi.mocked(crossFetch).mockRestore();
vi.mocked(fetch).mockRestore();
});

describe('complex example', () => {
Expand Down
Loading
Loading