Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get rid of axios dependency in the Upgrade Assistant tests. #127122

Merged
merged 3 commits into from
Mar 15, 2022

Conversation

azasypkin
Copy link
Member

@azasypkin azasypkin commented Mar 8, 2022

Summary

In #111655 (comment) we're trying to upgrade axios, but are blocked because of this axios usage in the tests (axios just happened to have methods signature similar to HttpSetup, but it's technically incorrect and quite fragile):

🚫 const httpSetup = axios.create({ adapter: axiosXhrAdapter }) as unknown as HttpSetup; 🚫

In this PR I switched Upgrade Assistant tests to use HttpSetup mock instead of axios + sinon server mock preserving test helpers shape as much as possible. Feel free to scratch this PR if you have a better approach in mind!

Re-merged after revert via: #127784

@azasypkin azasypkin marked this pull request as ready for review March 8, 2022 11:29
@azasypkin azasypkin requested a review from a team as a code owner March 8, 2022 11:29
@azasypkin azasypkin added auto-backport Deprecated - use backport:version if exact versions are needed release_note:skip Skip the PR/issue when compiling release notes v7.17.2 v8.2.0 labels Mar 8, 2022
Copy link
Member

@sabarasaba sabarasaba left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this @azasypkin! Most changes lgtm, though I left some questions/observations that I would like to get your opinion on 🎸

httpRequestsMockHelpers.setLoadDeprecationLoggingResponse(getLoggingResponse(true));
testBed = await setupESDeprecationLogsPage();
mockEnvironment = setupEnvironment();
mockEnvironment.httpRequestsMockHelpers.setLoadDeprecationLoggingResponse(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think of destructing httpRequestsMockHelpers from mockEnvironment? I think that will reduce the noise of the diff and also make things easier to follow since the changes will be scoped to the setup of the test rather the implementation of each.

I think this change can be applied throughout most of the test files in this PR.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, good point, let me do that!

{ 'Content-Type': 'application/json' },
JSON.stringify(body),
]);
const mockResponse = (method: HttpMethod, path: string, response?: unknown, error?: unknown) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love it! But was wondering if we should perhaps move all of this to a shared place (perhaps kbn-test) from which we could re-use this in other apps also? I think would also be cool if we could optionally specify headers too!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see your point, but the approach you use in UA and that was copied to few other plugins (additional abstraction over existing Core's mocks) is more like an exception and not something we (Platform) recommend. It's fine for a simple use case like you have, but if you need to customize response behavior (e.g. reply with different mock response based on parameters etc.) it becomes harder than using the Core's mock directly and also it's a bit harder to follow for people outside of the team.

So I'd rather localize this approach only inside plugins that opted to use it for now, until we have enough evidence that it can be useful for the majority of the plugins.

What do you think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense 👍🏼 , I think most of the platform-management apps use this pattern but let's keep an eye open for the other kibana apps and see if this is something more teams could benefit from.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, sounds good 👍

expect(server.requests.length).toBe(totalRequests + 4);
expect(server.requests[server.requests.length - 4].url).toBe(
`${API_BASE_PATH}/es_deprecations`
expect(mockEnvironment.httpSetup.get).toHaveBeenCalledWith(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we are to refactor out mockResponse into kbn-test I think would be nice to have some sort of mechanism to access an array of requests made like what sinnon provides, given that you can choose what to match against rather than having to have an ordered list of expect get toHaveBeenCalledWith

@kibana-ci
Copy link
Collaborator

💚 Build Succeeded

Metrics [docs]

✅ unchanged

History

To update your PR or re-run it, just comment with:
@elasticmachine merge upstream

@azasypkin azasypkin requested a review from sabarasaba March 14, 2022 16:40
Copy link
Member

@sabarasaba sabarasaba left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

latest lgtm, thanks @azasypkin!

@azasypkin azasypkin merged commit 53420d8 into elastic:main Mar 15, 2022
@azasypkin
Copy link
Member Author

Thanks for the review @sabarasaba !

@azasypkin azasypkin deleted the issue-xxx-axios-upgrade-assistant branch March 15, 2022 09:41
@kibanamachine
Copy link
Contributor

💔 All backports failed

Status Branch Result
7.17 Backport failed because of merge conflicts

Manual backport

To create the backport manually run:

node scripts/backport --pr 127122

Questions ?

Please refer to the Backport tool documentation

azasypkin added a commit that referenced this pull request Mar 15, 2022
…127122) (#127716)

* Get rid of `axios` dependency in the Upgrade Assistant tests. (#127122)

(cherry picked from commit 53420d8)

# Conflicts:
#	x-pack/plugins/upgrade_assistant/__jest__/client_integration/es_deprecation_logs/es_deprecation_logs.helpers.ts
#	x-pack/plugins/upgrade_assistant/__jest__/client_integration/es_deprecations/es_deprecations.helpers.ts
#	x-pack/plugins/upgrade_assistant/__jest__/client_integration/helpers/setup_environment.tsx
#	x-pack/plugins/upgrade_assistant/__jest__/client_integration/kibana_deprecations/kibana_deprecations.helpers.ts
#	x-pack/plugins/upgrade_assistant/__jest__/client_integration/overview/overview.helpers.ts

* Fix formatting.
jbudz added a commit that referenced this pull request Mar 15, 2022
@jbudz
Copy link
Member

jbudz commented Mar 15, 2022

@azasypkin FYI I reverted this at 7b86f00 due to a conflict with #126693 in main. It looks like a fix is in progress at #127779

@jbudz jbudz added the reverted label Mar 15, 2022
@azasypkin
Copy link
Member Author

@azasypkin FYI I reverted this at 7b86f00 due to a conflict with #126693 in main. It looks like a fix is in progress at #127779

Ugh, sorry about that. The fix won't work without the main PR - I'll re-create it including the changes from #127779

@sabarasaba
Copy link
Member

Thanks @azasypkin! I'll go ahead and close my pr then, ping me when you have this other PR open and I can have a look 👍🏼

@azasypkin
Copy link
Member Author

Thanks @azasypkin! I'll go ahead and close my pr then, ping me when you have this other PR open and I can have a look 👍🏼

Thanks, will do!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auto-backport Deprecated - use backport:version if exact versions are needed release_note:skip Skip the PR/issue when compiling release notes reverted v7.17.2 v8.2.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants