Skip to content

Commit 820015b

Browse files
authored
feat!: decouple from cross-fetch (#24)
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.
1 parent 8686d82 commit 820015b

19 files changed

+1072
-954
lines changed

.eslintrc.cjs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@ module.exports = {
88
parser: '@typescript-eslint/parser',
99
parserOptions: {
1010
tsconfigRootDir: __dirname,
11-
project: './tsconfig.eslint.json',
12-
// Because we are setting "type": "module" in our package.json, all `.js` files are treated as modules
13-
// Sometimes we will want a commonjs file, like this eslint config, in which case we use the .cjs extension
14-
extraFileExtensions: ['.cjs'],
11+
project: './tsconfig.json',
1512
},
1613
plugins: ['@typescript-eslint'],
1714
rules: {

.github/workflows/nodejs.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,6 @@ jobs:
2525
node-version: ${{ matrix.node-version }}
2626
- run: npm install
2727
- run: npm run lint
28-
- run: npm run tsc
29-
- run: npm test
28+
- run: npm run format:check
29+
- run: npm run build
30+
- run: npm run test

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ npm-debug.log
33
yarn-error.log
44
coverage
55
.idea
6+
dist

README.md

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ requests. It's easy to setup and you don't need a library like `nock` to get goi
1313
for mocking under the surface. This means that any of the `vi.fn()` methods are also available. For more information on
1414
the vitest mock API, check their docs [here](https://vitest.dev/guide/mocking.html)
1515

16-
It currently supports the mocking with the [`cross-fetch`](https://www.npmjs.com/package/cross-fetch) polyfill, so it
17-
supports Node.js and any browser-like runtime.
16+
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,
17+
which should be present in all modern runtimes and browsers.
1818

1919
## Contents
2020

@@ -631,8 +631,6 @@ the most used ones.
631631
```js
632632
// api.js
633633

634-
import 'cross-fetch';
635-
636634
export function APIRequest(who) {
637635
if (who === 'facebook') {
638636
return fetch('https://facebook.com');
@@ -749,16 +747,14 @@ For convenience, all the conditional mocking functions also accept optional para
749747
#### Conditional Mocking examples
750748

751749
```js
752-
import crossFetch from 'cross-fetch';
753-
754-
vi.mock('cross-fetch', async () => {
755-
const crossFetchActual = await requireActual('cross-fetch');
750+
vi.mock('fetch', async () => {
751+
const fetchActual = globalThis.fetch;
756752
const mocked = {};
757-
for (const key of Object.keys(crossFetchActual)) {
758-
if (typeof crossFetchActual[key] === 'function') {
759-
mocked[key] = vi.fn(crossFetchActual[key]);
753+
for (const key of Object.keys(fetchActual)) {
754+
if (typeof fetchActual[key] === 'function') {
755+
mocked[key] = vi.fn(fetchActual[key]);
760756
} else {
761-
mocked[key] = crossFetchActual[key];
757+
mocked[key] = fetchActual[key];
762758
}
763759
}
764760
return mocked;
@@ -768,18 +764,18 @@ describe('conditional mocking', () => {
768764
const realResponse = 'REAL FETCH RESPONSE'
769765
const mockedDefaultResponse = 'MOCKED DEFAULT RESPONSE'
770766
const testUrl = defaultRequestUri
771-
let crossFetchSpy
767+
let fetchSpy
772768
beforeEach(() => {
773769
fetch.resetMocks()
774770
fetch.mockResponse(mockedDefaultResponse)
775-
vi.mocked(crossFetch)
771+
vi.mocked(fetch)
776772
.mockImplementation(async () =>
777773
Promise.resolve(new Response(realResponse))
778774
)
779775
})
780776

781777
afterEach(() => {
782-
vi.mocked(crossFetch).mockRestore()
778+
vi.mocked(fetch).mockRestore()
783779
})
784780

785781
const expectMocked = async (uri, response = mockedDefaultResponse) => {
@@ -981,16 +977,14 @@ describe('conditional mocking', () => {
981977
```
982978
983979
```js
984-
import crossFetch from 'cross-fetch';
985-
986-
vi.mock('cross-fetch', async () => {
987-
const crossFetchActual = await requireActual('cross-fetch');
980+
vi.mock('fetch', async () => {
981+
const fetchActual = globalThis.fetch;
988982
const mocked = {};
989-
for (const key of Object.keys(crossFetchActual)) {
990-
if (typeof crossFetchActual[key] === 'function') {
991-
mocked[key] = vi.fn(crossFetchActual[key]);
983+
for (const key of Object.keys(fetchActual)) {
984+
if (typeof fetchActual[key] === 'function') {
985+
mocked[key] = vi.fn(fetchActual[key]);
992986
} else {
993-
mocked[key] = crossFetchActual[key];
987+
mocked[key] = fetchActual[key];
994988
}
995989
}
996990
return mocked;
@@ -1007,15 +1001,15 @@ describe('conditional mocking complex', () => {
10071001
const realResponse = 'REAL FETCH RESPONSE';
10081002
const mockedDefaultResponse = 'MOCKED DEFAULT RESPONSE';
10091003
const testUrl = defaultRequestUri;
1010-
let crossFetchSpy;
1004+
let fetchSpy;
10111005
beforeEach(() => {
10121006
fetch.resetMocks();
10131007
fetch.mockResponse(mockedDefaultResponse);
1014-
vi.mocked(crossFetch).mockImplementation(async () => Promise.resolve(new Response(realResponse)));
1008+
vi.mocked(fetch).mockImplementation(async () => Promise.resolve(new Response(realResponse)));
10151009
});
10161010

10171011
afterEach(() => {
1018-
vi.mocked(crossFetch).mockRestore();
1012+
vi.mocked(fetch).mockRestore();
10191013
});
10201014

10211015
describe('complex example', () => {

0 commit comments

Comments
 (0)