-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
[Uptime] Remove prototype overwrite in unit test #73709
Conversation
Pinging @elastic/uptime (Team:uptime) |
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', | ||
}, | ||
}; | ||
}; | ||
}); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
…th-new-testing-utils
mockMoment(); | ||
mockMomentTimezone(); | ||
mockDataPlugin(); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 @@ | |||
/* |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
mockMoment(); | ||
mockMomentTimezone(); | ||
mockDataPlugin(); |
There was a problem hiding this comment.
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', () => { |
There was a problem hiding this comment.
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')); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM !!
@shahzad31 can you please have a look at 2a22f29 and a98b048 to make sure you're still 👍 on this. |
Looks good !! |
💚 Build SucceededMetrics [docs]Async chunks
History
To update your PR or re-run it, just comment with: |
* 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]>
* 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]>
* 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]>
* 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]>
Backported to: |
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 withjest.spyOn
.