Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Allow optionally() to be called with a value, specifying if the…
… mock should be optional When setting up a mock, you can call `optionally()` to specify that the mock is __optional__, meaning that it won't be returned in `nock.pendingMocks()` and it won't affect the result of `nock.isDone()`. You have to call `optionally()` before you call a method like `reply()` to specify the response, because at that point the mock is persisted and you can no longer touch it (that is to say, `reply()` returns the scope rather than the mock). This part of Nock's API makes life painful if you want to have a mock which is configurable as either optional or required. The way that mocks are chained together makes your code long-winded and hard to read: ```js const getLoginMock = ({ optional = false }) => { const mock = nock('https://supercoolauthservice.com') .post('/login'); if (optional) { mock = mock.optionally(); } mock.reply(200); } ``` This change makes life simpler, allowing you to specify when you call `optionally()` whether you want the mock to be optional or not: ```js const getLoginMock = ({ optional = false }) => { nock('https://supercoolauthservice.com') .post('/login') .optionally(optional) .reply(200); } ```
- Loading branch information