Skip to content

Commit

Permalink
feat!: decouple from cross-fetch (#24)
Browse files Browse the repository at this point in the history
All right, this PR includes:

- Moving away from `cross-fetch` by just patching the global fetch which should be present in all modern runtimes and browsers. That would close issue #22 and #23.
- It will now force Node 18, which closes issue #21 and removes the need to patch the `DOMException`. 
- The source code is now written in TypeScript and transpiled to JavaScript into the `dist` directory, including the type definitions and source maps.
  • Loading branch information
dirkluijk authored Oct 24, 2024
1 parent 8686d82 commit 820015b
Show file tree
Hide file tree
Showing 19 changed files with 1,072 additions and 954 deletions.
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.
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,
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

0 comments on commit 820015b

Please sign in to comment.