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

[Uptime] Remove prototype overwrite in unit test #73709

Merged

Conversation

justinkambic
Copy link
Contributor

Summary

Follow-up to #73531, we are looking to avoid directly overwriting the prototype of moment in a test file. I have also wrapped a few other direct prototype overwrites with jest.spyOn.

@justinkambic justinkambic added v8.0.0 Team:Uptime - DEPRECATED Synthetics & RUM sub-team of Application Observability release_note:skip Skip the PR/issue when compiling release notes test-unit v7.10.0 7.9.0 labels Jul 29, 2020
@justinkambic justinkambic requested a review from a team as a code owner July 29, 2020 16:39
@justinkambic justinkambic self-assigned this Jul 29, 2020
@elasticmachine
Copy link
Contributor

Pinging @elastic/uptime (Team:uptime)

Comment on lines 12 to 33
jest.mock('moment-timezone', () => {
return function () {
return { tz: { guess: () => 'America/New_York' } };
};
});

jest.mock('moment', () => {
return function () {
return { fromNow: () => 'a year ago' };
};
});

jest.mock('../../../../../../../../src/plugins/data/public', () => {
return function () {
return {
esKuery: {
fromKueryExpression: () => 'an ast',
toElasticsearchQuery: () => 'an es query',
},
};
};
});
Copy link
Contributor

Choose a reason for hiding this comment

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

can we move these mocks to a test_util file, so that these can also be used in other places.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you mean the custom module factories? So use would look like:

import { momentMockFactory } from './util';
jest.mock('moment', momentMockFactory);

We could modularize them, but they're kind of specific to the needs of this test file. moment-timezone probably won't need to be mocked in most situations, for example, and there are lots of cases where we'll need more than just fromNow on moment mocks.

Copy link
Contributor

Choose a reason for hiding this comment

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

can we consolidate all these mocks, so we can use it in other places as well https://github.com/elastic/kibana/blob/master/x-pack/plugins/uptime/public/lib/helper/test_helpers.ts

@lukasolson lukasolson added v7.9.0 and removed 7.9.0 labels Jul 29, 2020
Comment on lines +16 to +18
mockMoment();
mockMomentTimezone();
mockDataPlugin();
Copy link
Contributor

Choose a reason for hiding this comment

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

instead of running these in each test, do you think we should move these as part of jest setup?

in uptime jest.config.js?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We should do this in a follow-up: elastic/uptime#283

@@ -0,0 +1,187 @@
/*
Copy link
Contributor

Choose a reason for hiding this comment

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

i think file needs to be removed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah it was my mistake in a previous merge, I went in and got rid of it.

Comment on lines +16 to +18
mockMoment();
mockMomentTimezone();
mockDataPlugin();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We should do this in a follow-up: elastic/uptime#283

@@ -48,18 +58,13 @@ describe('PingHistogram component', () => {
},
};

it('shallow renders the component without errors', () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I decided this test wasn't worth keeping. It asserted that the PingHistogramComponent contained the props that were passed to it, and not much else.

Rendering these components doesn't yield SVG or any other method of ensuring the data is rendered by Elastic Charts, but we also should not be testing those capabilities in our repo.


fireEvent.mouseOver(locationsContainer);

await waitFor(() => screen.getByText('Up in BerlinDown in Islamabad, st-paul'));
Copy link
Contributor

Choose a reason for hiding this comment

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

Should there be a space between?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good question, I'll have a look in a real-world case. It could just be the way the raw html looks.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It was indeed a bug; we could've tackled it in a separate issue but I fixed it in 2a22f29.

Copy link
Contributor

@shahzad31 shahzad31 left a comment

Choose a reason for hiding this comment

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

LGTM !!

@justinkambic
Copy link
Contributor Author

@shahzad31 can you please have a look at 2a22f29 and a98b048 to make sure you're still 👍 on this.

@shahzad31
Copy link
Contributor

@shahzad31 can you please have a look at 2a22f29 and a98b048 to make sure you're still 👍 on this.

Looks good !!

@kibanamachine
Copy link
Contributor

💚 Build Succeeded

Metrics [docs]

Async chunks

Total size of all lazy-loaded chunks that will be downloaded as the user navigates the app

id before after diff
uptime 1.1MB 1.1MB +353.0B

History

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

@justinkambic justinkambic merged commit 1094274 into elastic:master Jan 12, 2021
justinkambic added a commit to justinkambic/kibana that referenced this pull request Jan 12, 2021
* Remove prototype overwrite in unit test.

* Wrap calls to prototype functions with jest.spyOn.

* add additional component test helpers

* add test examples

* uptime testing utils remove custom prefix from props and parameter options

* skip executed step tests

* adjust MlJobLink test

* add testing util interfaces

* Move updated test files to correct locations.

* Move other test file to correct location.

* Revert unintended changes from merge.

* Revert unintended changes from merge.

* update mock core

* combine wrappers into one custom render function

* Move mock helpers to helper file. Update snapshots.

* Refactor tests to use RTL over enzyme.

* Refactor \`PingHistogram\` component tests to prefer RTL to Enzyme.

* Drop obsolete snapshot file.

* Remove obsolete file leftover from merge error.

* Fix outdated import path.

* Prefer custom render to vanilla.

* Fix formatting issue uncovered by unit test, and update test assertion.

* Add test for single location.

Co-authored-by: Dominique Clarke <[email protected]>
Co-authored-by: Kibana Machine <[email protected]>
justinkambic added a commit to justinkambic/kibana that referenced this pull request Jan 12, 2021
* Remove prototype overwrite in unit test.

* Wrap calls to prototype functions with jest.spyOn.

* add additional component test helpers

* add test examples

* uptime testing utils remove custom prefix from props and parameter options

* skip executed step tests

* adjust MlJobLink test

* add testing util interfaces

* Move updated test files to correct locations.

* Move other test file to correct location.

* Revert unintended changes from merge.

* Revert unintended changes from merge.

* update mock core

* combine wrappers into one custom render function

* Move mock helpers to helper file. Update snapshots.

* Refactor tests to use RTL over enzyme.

* Refactor \`PingHistogram\` component tests to prefer RTL to Enzyme.

* Drop obsolete snapshot file.

* Remove obsolete file leftover from merge error.

* Fix outdated import path.

* Prefer custom render to vanilla.

* Fix formatting issue uncovered by unit test, and update test assertion.

* Add test for single location.

Co-authored-by: Dominique Clarke <[email protected]>
Co-authored-by: Kibana Machine <[email protected]>
justinkambic added a commit that referenced this pull request Jan 13, 2021
* Remove prototype overwrite in unit test.

* Wrap calls to prototype functions with jest.spyOn.

* add additional component test helpers

* add test examples

* uptime testing utils remove custom prefix from props and parameter options

* skip executed step tests

* adjust MlJobLink test

* add testing util interfaces

* Move updated test files to correct locations.

* Move other test file to correct location.

* Revert unintended changes from merge.

* Revert unintended changes from merge.

* update mock core

* combine wrappers into one custom render function

* Move mock helpers to helper file. Update snapshots.

* Refactor tests to use RTL over enzyme.

* Refactor \`PingHistogram\` component tests to prefer RTL to Enzyme.

* Drop obsolete snapshot file.

* Remove obsolete file leftover from merge error.

* Fix outdated import path.

* Prefer custom render to vanilla.

* Fix formatting issue uncovered by unit test, and update test assertion.

* Add test for single location.

Co-authored-by: Dominique Clarke <[email protected]>
Co-authored-by: Kibana Machine <[email protected]>

Co-authored-by: Dominique Clarke <[email protected]>
Co-authored-by: Kibana Machine <[email protected]>
justinkambic added a commit that referenced this pull request Jan 13, 2021
* Remove prototype overwrite in unit test.

* Wrap calls to prototype functions with jest.spyOn.

* add additional component test helpers

* add test examples

* uptime testing utils remove custom prefix from props and parameter options

* skip executed step tests

* adjust MlJobLink test

* add testing util interfaces

* Move updated test files to correct locations.

* Move other test file to correct location.

* Revert unintended changes from merge.

* Revert unintended changes from merge.

* update mock core

* combine wrappers into one custom render function

* Move mock helpers to helper file. Update snapshots.

* Refactor tests to use RTL over enzyme.

* Refactor \`PingHistogram\` component tests to prefer RTL to Enzyme.

* Drop obsolete snapshot file.

* Remove obsolete file leftover from merge error.

* Fix outdated import path.

* Prefer custom render to vanilla.

* Fix formatting issue uncovered by unit test, and update test assertion.

* Add test for single location.

Co-authored-by: Dominique Clarke <[email protected]>
Co-authored-by: Kibana Machine <[email protected]>

Co-authored-by: Dominique Clarke <[email protected]>
Co-authored-by: Kibana Machine <[email protected]>
@justinkambic
Copy link
Contributor Author

@justinkambic justinkambic deleted the uptime_ping-histogram-jest-mock-moment branch January 13, 2021 12:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release_note:skip Skip the PR/issue when compiling release notes Team:Uptime - DEPRECATED Synthetics & RUM sub-team of Application Observability test-unit v8.0.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants