Skip to content

Commit

Permalink
Create onNoMatch=throwException option to throw exception when could …
Browse files Browse the repository at this point in the history
…not find mock for requested url.
  • Loading branch information
rafaelcaviquioli committed Apr 23, 2020
1 parent 4d59112 commit a52b450
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 1 deletion.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,24 @@ var mock = new MockAdapter(axiosInstance, { onNoMatch: "passthrough" });
mock.onAny("/foo").reply(200);
```

Using `onNoMatch` option with `throwException` to throw an exception when a request is made without match any handler. It's helpful to debug your test mocks.

```js
var mock = new MockAdapter(axiosInstance, { onNoMatch: "throwException" });

mock.onAny("/foo").reply(200);

axios.get("/unexistent-path");

// Exception message on console:
//
// Could not find mock for:
// {
// "method": "get",
// "url": "http://localhost/unexistent-path"
// }
```

As of 1.7.0, `reply` function may return a Promise:

```js
Expand Down
2 changes: 2 additions & 0 deletions src/handle_request.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ function handleRequest(mockAdapter, resolve, reject, config) {
case "passthrough":
mockAdapter.originalAdapter(config).then(resolve, reject);
break;
case 'throwException':
throw utils.createCouldNotFindMockError(config);
default:
utils.settle(
resolve,
Expand Down
12 changes: 11 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ function isBodyMatching(body, requiredBody) {
var parsedBody;
try {
parsedBody = JSON.parse(body);
} catch (e) {}
} catch (e) { }

return isObjectMatching(parsedBody ? parsedBody : body, requiredBody);
}
Expand Down Expand Up @@ -156,6 +156,15 @@ function createAxiosError(message, config, response, code) {
return error;
}

function createCouldNotFindMockError(config) {
var message = "Could not find mock for: \n" + JSON.stringify(config, ['method', 'url'], 2);
var error = new Error(message);
error.isCouldNotFindMockError = true;
error.url = config.url;
error.method = config.method;
return error;
}

module.exports = {
find: find,
findHandler: findHandler,
Expand All @@ -168,4 +177,5 @@ module.exports = {
isBuffer: isBuffer,
isEqual: isEqual,
createAxiosError: createAxiosError,
createCouldNotFindMockError: createCouldNotFindMockError
};
50 changes: 50 additions & 0 deletions test/throw_exception_on_no_match.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
var axios = require("axios");
var expect = require("chai").expect;

var MockAdapter = require("../src");

describe("onNoMatch=throwException option tests (requires Node)", function () {
var instance;
var mock;

beforeEach(function () {
instance = axios.create();
mock = new MockAdapter(instance, { onNoMatch: "throwException" });
});

it("allows selective mocking", function () {
mock.onGet("/foo").reply(200, "bar");
mock.onGet("/error").reply(200, "success");

return Promise.all([
instance.get("/foo").then(function (response) {
expect(response.status).to.equal(200);
expect(response.data).to.equal("bar");
}),
instance.get("/error").then(function (response) {
expect(response.status).to.equal(200);
expect(response.data).to.equal("success");
}),
]);
});

it("handles errors correctly when could not find mock for requested url", function () {
var expectedUrl = 'http://127.0.0.1/unexistent_path';
var expectedMethod = "get";

return instance
.get(expectedUrl)
.then(function () {
// The server should've returned an error
expect(false).to.be.true;
})
.catch(function (error) {
expect(error).to.have.nested.property("isCouldNotFindMockError", true);
expect(error).to.have.nested.property("method", expectedMethod);
expect(error).to.have.nested.property("url", expectedUrl);
expect(error.message).to.contain("Could not find mock for");
expect(error.message).to.contain(expectedMethod);
expect(error.message).to.contain(expectedUrl);
});
});
});

0 comments on commit a52b450

Please sign in to comment.