Skip to content

Commit ca4bfa1

Browse files
dsebastienloris
authored and
loris
committed
Improved TS support (#99)
1 parent b72838f commit ca4bfa1

11 files changed

+335
-34
lines changed

.gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,3 @@ node_modules
22
npm-debug.log
33
yarn-error.log
44
coverage
5-
.idea

.npmignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
tests
2-
yarn.lock
2+
yarn.lock

README.md

+31
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,37 @@ Add the setupFile to your jest config in `package.json`:
4848
}
4949
```
5050

51+
### TypeScript guide
52+
53+
If you are using TypeScript, then you can follow the instructions below instead.
54+
55+
```
56+
$ npm install --save-dev jest-fetch-mock
57+
```
58+
59+
Create a `setupJest.ts` file to setup the mock :
60+
61+
```ts
62+
import {GlobalWithFetchMock} from "jest-fetch-mock";
63+
64+
const customGlobal: GlobalWithFetchMock = global as GlobalWithFetchMock;
65+
customGlobal.fetch = require('jest-fetch-mock');
66+
customGlobal.fetchMock = customGlobal.fetch;
67+
```
68+
69+
Add the setupFile to your jest config in `package.json`:
70+
71+
```JSON
72+
"jest": {
73+
"automock": false,
74+
"setupFiles": [
75+
"./setupJest.ts"
76+
]
77+
}
78+
```
79+
80+
With this done, you'll have `fetch` and `fetchMock` available on the global scope. Fetch will be used as usual by your code and you'll use `fetchMock` in your tests.
81+
5182
### Using with Create-React-App
5283

5384
If you are using [Create-React-App](https://github.com/facebookincubator/create-react-app) (CRA), the code for `setupTest.js` above should be placed into `src/setupTests.js` in the root of your project. CRA automatically uses this filename by convention in the Jest configuration it generates. Similarly, changing to your `package.json` is not required as CRA handles this when generating your Jest configuration.

package.json

+9-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
"version": "2.0.1",
44
"description": "fetch mock for jest",
55
"main": "src/index.js",
6-
"types": "src/index.d.ts",
6+
"types": "types",
77
"scripts": {
8-
"test": "jest"
8+
"test": "jest && npm run tsc && npm run dtslint",
9+
"dtslint": "dtslint types",
10+
"tsc": "tsc"
911
},
1012
"repository": {
1113
"type": "git",
@@ -27,11 +29,15 @@
2729
"promise-polyfill": "^7.1.1"
2830
},
2931
"devDependencies": {
32+
"@types/jest": "^23.3.10",
33+
"@types/node": "^10.12.10",
3034
"babel-core": "^6.26.3",
3135
"babel-jest": "^23.4.2",
3236
"babel-preset-env": "^1.7.0",
37+
"dtslint": "^0.3.0",
3338
"jest": "^23.5.0",
34-
"regenerator-runtime": "^0.12.1"
39+
"regenerator-runtime": "^0.12.1",
40+
"typescript": "^3.2.1"
3541
},
3642
"prettier": {
3743
"semi": false,

src/index.d.ts

-24
This file was deleted.

tsconfig.json

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"extends": "./types/tsconfig.json",
3+
"compilerOptions": {
4+
"baseUrl": ".",
5+
"rootDir": "./types",
6+
"paths": {
7+
"jest-fetch-mock": [
8+
"./"
9+
]
10+
}
11+
}
12+
}

tslint.json

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "extends": "dtslint/dt.json" }

types/index.d.ts

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// TypeScript Version: 2.3
2+
import Global = NodeJS.Global;
3+
import "jest";
4+
5+
declare global {
6+
const fetchMock: FetchMock;
7+
namespace NodeJS {
8+
interface Global {
9+
fetch: FetchMock;
10+
}
11+
}
12+
}
13+
14+
export interface GlobalWithFetchMock extends Global {
15+
fetchMock: FetchMock;
16+
fetch: FetchMock;
17+
}
18+
19+
export interface FetchMock extends jest.MockInstance<any> {
20+
(input?: string | Request, init?: RequestInit): Promise<Response>;
21+
22+
mockResponse(body: BodyOrFunction, init?: MockParams): FetchMock;
23+
mockResponseOnce(body: BodyOrFunction, init?: MockParams): FetchMock;
24+
once(body: BodyOrFunction, init?: MockParams): FetchMock;
25+
mockResponses(...responses: Array<(BodyOrFunction | [BodyOrFunction, MockParams])>): FetchMock;
26+
mockReject(error?: ErrorOrFunction): FetchMock;
27+
mockRejectOnce(error?: ErrorOrFunction): FetchMock;
28+
resetMocks(): void;
29+
}
30+
31+
// reference: https://github.github.io/fetch/#Response
32+
export interface MockParams {
33+
status?: number;
34+
statusText?: string;
35+
headers?: string[][] | { [key: string]: string }; // HeadersInit
36+
url?: string;
37+
}
38+
39+
export interface MockResponseInit extends MockParams {
40+
body?: string;
41+
}
42+
43+
export type BodyOrFunction = string | MockResponseInitFunction;
44+
export type ErrorOrFunction = Error | MockResponseInitFunction;
45+
46+
export type MockResponseInitFunction = () => Promise<MockResponseInit>;

types/test.ts

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { MockResponseInit } from 'jest-fetch-mock';
2+
3+
fetchMock.mockResponse(JSON.stringify({foo: "bar"}));
4+
fetchMock.mockResponse(JSON.stringify({foo: "bar"}), {
5+
status: 200,
6+
headers: [
7+
["Content-Type", "application/json"]
8+
]
9+
});
10+
fetchMock.mockResponse(JSON.stringify({foo: "bar"}), {});
11+
fetchMock.mockResponse(someAsyncHandler);
12+
fetchMock.mockResponse(someAsyncHandler, {});
13+
14+
fetchMock.mockResponseOnce(JSON.stringify({foo: "bar"}));
15+
fetchMock.mockResponseOnce(JSON.stringify({foo: "bar"}), {
16+
status: 200,
17+
headers: [
18+
["Content-Type", "application/json"]
19+
]
20+
});
21+
fetchMock.mockResponseOnce(JSON.stringify({foo: "bar"}), {});
22+
fetchMock.mockResponseOnce(someAsyncHandler);
23+
fetchMock.mockResponseOnce(someAsyncHandler, {});
24+
25+
fetchMock.once(JSON.stringify({foo: "bar"}));
26+
fetchMock.once(JSON.stringify({foo: "bar"}), {
27+
status: 200,
28+
headers: [
29+
["Content-Type", "application/json"]
30+
]
31+
});
32+
fetchMock.once(JSON.stringify({foo: "bar"}), {});
33+
fetchMock.once(someAsyncHandler);
34+
fetchMock.once(someAsyncHandler, {});
35+
36+
fetchMock.mockResponses(JSON.stringify({}), JSON.stringify({foo: "bar"}));
37+
fetchMock.mockResponses(someAsyncHandler, someAsyncHandler);
38+
fetchMock.mockResponses(JSON.stringify({}), someAsyncHandler);
39+
fetchMock.mockResponses(someAsyncHandler, JSON.stringify({}));
40+
fetchMock.mockResponses([someAsyncHandler, {status: 200}]);
41+
fetchMock.mockResponses([JSON.stringify({foo: "bar"}), {status: 200}]);
42+
fetchMock.mockResponses(
43+
[someAsyncHandler, {status: 200}],
44+
[JSON.stringify({foo: "bar"}), {status: 200}]
45+
);
46+
47+
fetchMock.mockReject(new Error("oops"));
48+
fetchMock.mockReject(someAsyncHandler);
49+
50+
fetchMock.mockRejectOnce(new Error("oops"));
51+
fetchMock.mockRejectOnce(someAsyncHandler);
52+
fetchMock.resetMocks();
53+
54+
async function someAsyncHandler(): Promise<MockResponseInit> {
55+
return {
56+
status: 200,
57+
body: JSON.stringify({foo: "bar"})
58+
};
59+
}

types/tsconfig.json

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"compilerOptions": {
3+
"module": "commonjs",
4+
"lib": [
5+
"es2015",
6+
"dom"
7+
],
8+
"baseUrl": ".",
9+
"paths": {
10+
"jest-fetch-mock": [
11+
"."
12+
]
13+
},
14+
"noImplicitAny": true,
15+
"noImplicitThis": true,
16+
"strictNullChecks": true,
17+
"strictFunctionTypes": true,
18+
"noEmit": true,
19+
"forceConsistentCasingInFileNames": true
20+
}
21+
}

0 commit comments

Comments
 (0)