[Enterprise Search] Fix/update MockRouter helper to return specific routes/paths #82682
Merged
cee-chen merged 3 commits intoelastic:masterfrom Nov 5, 2020
Merged
[Enterprise Search] Fix/update MockRouter helper to return specific routes/paths #82682cee-chen merged 3 commits intoelastic:masterfrom
cee-chen merged 3 commits intoelastic:masterfrom
Conversation
Contributor
Author
|
I'll add that it's also possible to get around this simply by never putting more than 1 router method type per register function, for example: export function registerSomeRoute(...) {
router.get(
{
path: '/api/app_search/example_one',
validate: false,
},
enterpriseSearchRequestHandler.createRequest({
path: '/as/example_one',
})
};
});
export function registerAnotherRoute(...) {
router.get(
{
path: '/api/app_search/example_two',
validate: false,
},
enterpriseSearchRequestHandler.createRequest({
path: '/as/example_two',
})
};
});But I thought that fixing/modifying our helper would be more flexible & allow us to register multiple routes per function. Definitely open to more thoughts though, let me know what you think! |
Contributor
Author
|
Also @scottybollinger I know this impacts your test PR a decent amount, I plan on merging this after your PR and rebasing against it |
Member
|
💯 For this change. I've definitely had the feeling you've described:
|
…rations of the same method - This fix allows us to specify the route call we're testing via a path param
- e.g., in case a path gets typoed
68352e2 to
bed7838
Compare
Contributor
💚 Build SucceededMetrics [docs]
History
To update your PR or re-run it, just comment with: |
cee-chen
pushed a commit
that referenced
this pull request
Nov 5, 2020
…outes/paths (#82682) (#82781) * Fix tests failing for route files that have more than 2 router registrations of the same method - This fix allows us to specify the route call we're testing via a path param * Update all existing uses of MockRouter to pass path param * Add helpful error messaging - e.g., in case a path gets typoed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
I realized while doing some server API refactoring/testing that our MockRouter helper would fail/produce a false negative in the following example scenario:
Specifically, calling
MockRouter.callRoute()would never actually call the 2ndrouter.gethandler because of this line in the code:kibana/x-pack/plugins/enterprise_search/server/__mocks__/router.mock.ts
Line 50 in f065191
Each
router.getregistration creates another array item inmock.calls, so the static[0]index meant only the 1st of each type of router method in a singleregisterSomeRoutefunction was ever getting called 😬My solution/approach to this was adding a
pathparam to the MockRouter initialization, and searching the array for the correct/specific route path. This has the benefit of being more explicit about exactly which route we're testing in our test code - I'll admit there have been a few times when I've been writing tests where I'm like, "how does this mock router know what route I'm testing??" so hopefully this helps answer that.Checklist
Delete any items that are not applicable to this PR.