jest-fetch-mock bindings for BuckleScript in Reason
npm install --save-dev bs-jest-fetch-mock jest-fetch-mock
# or yarn...
yarn add -D bs-jest-fetch-mock jest-fetch-mock
Add following code to your Jest's setupFiles:
// jest.setup.js
require('jest-fetch-mock').enableMocks();
Add following code to your Jest's configuration:
// package.json
"jest": {
"automock": false,
"setupFiles": [
"./jest.setup.js"
]
}
Add to bsconfig.json
...
"bs-dependencies": [
+ "bs-jest-fetch-mock"
]
...
For more example, please refer to JestFetchMock_test.re
BsJestFetchMock
is the root namespace module.
string
JestFetchMock.mockResponse(`Str({|{ "body": "ok" }|}), Js.Undefined.empty);
string with init
JestFetchMock.mockResponse(
`Str({|{ "body": "ok" }|}),
Js.Undefined.return(
init(
~status=204,
~statusText="nothing for you",
~headers=Js.Dict.fromList([("Authorization", "Bearer <token>")]),
(),
),
),
);
function (with string)
JestFetchMock.mockResponse(
`FnStr(
req =>
if (Fetch.Request.url(req) == "http://parsed_url/") {
resolve({|{ "body": "ok" }|});
} else {
resolve("");
},
),
Js.Undefined.empty,
);
function (with response)
JestFetchMock.mockResponse(
`FnResp(
req =>
if (Fetch.Request.url(req) == "http://parsed_url/") {
response(
~body={|{ "body": "ok" }|},
~status=200,
~statusText="OK",
~headers=
Js.Dict.fromList([("Authorization", "Bearer <token>")]),
(),
)
|> resolve,
} else {
response(
~body="",
~status=418,
~statusText="I'm a teapot",
~headers=
Js.Dict.fromList([("Authorization", "Bearer <token>")]),
(),
)
|> resolve,
},
),
Js.Undefined.empty,
);
Same function signature as mockResponse
.
JestFetchMock.mockResponsesStr([|
({|"first body"|}, Js.Undefined.empty),
(
{|"second body"|},
Js.Undefined.return(
init(
~status=200,
~statusText="ok",
~headers=Js.Dict.fromList([("Authorization", "Bearer <token>")]),
(),
),
),
),
|]);
JestFetchMock.mockResponsesFn([|
(_req => Js.Promise.resolve({|"first body"|}), Js.Undefined.empty),
(
_req => Js.Promise.resolve({|"second body"|}),
Js.Undefined.return(
init(
~status=200,
~statusText="ok",
~headers=Js.Dict.fromList([("Authorization", "Bearer <token>")]),
(),
),
),
),
|]);
JestFetchMock.mockResponsesFnResp([|
(
_req =>
response(
~body={|"first body"|},
~status=418,
~statusText="I'm a teapot",
(),
)
|> resolve,
Js.Undefined.empty
),
(
_req =>
response(
~body={|"second body"|},
(),
)
|> resolve,
Js.Undefined.return(
init(
~status=200,
~statusText="ok",
~headers=Js.Dict.fromList([("Authorization", "Bearer <token>")]),
(),
),
),
),
|]);
string
JestFetchMock.mockReject(`Str({|{ "body": "ok" }|}));
function
JestFetchMock.mockReject(`FnStr(_req => Js.Promise.resolve({|{ "body": "ok" }|})));
Same function signature as mockReject
.
JestFetchMock.mockReject();
Same function signature as mockAbort
.
JestFetchMock.resetMocks();
npm test
this will compile and execute tests with bs-jest
Don't hesitate to open a PR with a new binding - while bumping up the amount of covered bindings in the README. There are tests, use them and write the most simple test you can think of to make sure that the bindings work correctly.