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

[Security Solution][Detections] Cleaning up mocks/tests #74920

Merged
merged 17 commits into from
Aug 24, 2020

Conversation

rylnd
Copy link
Contributor

@rylnd rylnd commented Aug 13, 2020

Summary

This is a chore PR that's a followup to #74582 and #75378 . Contents:

  • Followup to feedback on [Security Solution][Detections] Refactor ML calls for newest ML permissions #74582
  • Adds tests for useListsConfig hook
  • Adds .mock files in files/modules touched
  • Mocks cleanup
    • Removes most uses of any with unknown
    • Refactors our kibana_react mocks to be reusable
    • Replaces our kibana_core mocks with mocks provided by core (which were just delegating directly to anyway)
    • Removes other redundant mocks with existing ones
    • Removes unnecessary mocks usage in tests
      • unnecessary calls to clearAll, etc.

Checklist

Delete any items that are not applicable to this PR.

For maintainers

rylnd added 4 commits August 12, 2020 22:30
* Simpler mock factory that returns an object instead of a thunk
  * We can use mockReturnValue instead of mockImplementation to
  accomplish the same
  * Allows us to replace createStartServices mock
* Uses unknown instead of any for mocks
* Since our useKibana mock returns a consistent mock, we can modify its
return value instead of re-mocking the entire thing
* Removes unnecessary uses of clearing/resetting mocks
  * If your mocks are configured at the beginning of each test this is
  usually unnecessary.
  * I left one case of clearAllMocks in all_cases/index.test since it
  defined several mock functions that were persistent across tests, and
  it was easier than moving their definitions to a beforeEach
* Removes some unnecessary overrides that seemed due to storage
previously not being mocked
There's a good chance that the consumer might want the OTHER hook, so
let's make that discoverable.
@rylnd rylnd added chore Team:SIEM v8.0.0 release_note:skip Skip the PR/issue when compiling release notes v7.10.0 labels Aug 13, 2020
@rylnd rylnd self-assigned this Aug 13, 2020
@@ -97,23 +96,16 @@ describe('AllCases', () => {
});
/* eslint-enable no-console */
beforeEach(() => {
jest.resetAllMocks();
jest.clearAllMocks();
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 left this one instance of clearAllMocks as detailed in 0e5dfe3. Instead of defining mocks once and then clearing their state in a beforeEach, my preference is to instead define the mocks in the beforeEach. That's the pattern I followed for the other tests, this one was the exception.

const Proxy = (props: QueryBarComponentProps) => (
<TestProviders>
<KibanaWithStorageProvider services={{ storage: { get: jest.fn() } }}>
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 think these tests must have been written before our kibana_react mocks (used by TestProviders) included storage mocks; this code has been unnecessary since that was added.

@@ -66,61 +62,38 @@ export const createUseUiSettingMock = () => <T extends unknown = string>(
export const createUseUiSetting$Mock = () => {
const useUiSettingMock = createUseUiSettingMock();

return <T extends unknown = string>(
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 removed these generics as they were a) wrong and b) not necessary since these are mocks; consumers can modify/cast these as necessary to fit their need.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

One other note about these uiSettings mocks: they really shouldn't be needed, but we have >80 tests that rely on specific values being returned and I haven't yet had time to fix them individually.

@rylnd rylnd requested review from spong and cnasikas August 13, 2020 04:15
rylnd added 9 commits August 19, 2020 17:06
* adds mocks for the hooks upon which it depends
Leverages this mock factory in our manual mock for this hook.
We're trying to consolidate mocks to this pattern so that they're easier
to find and reuse.
This was only being consumed by our general createStartServicesMock.
This is just noise/tech debt, we should use the core mock directly when
we can.
Instead let's just reference the core mocks themselves.
@rylnd rylnd requested a review from yctercero August 20, 2020 00:52
@rylnd rylnd marked this pull request as ready for review August 20, 2020 17:27
@rylnd rylnd requested review from a team as code owners August 20, 2020 17:27
@elasticmachine
Copy link
Contributor

Pinging @elastic/siem (Team:SIEM)

@rylnd
Copy link
Contributor Author

rylnd commented Aug 20, 2020

@elasticmachine merge upstream

@rylnd
Copy link
Contributor Author

rylnd commented Aug 20, 2020

@elasticmachine merge upstream

@FrankHassanabad
Copy link
Contributor

@elasticmachine merge upstream

@@ -28,7 +27,7 @@ jest.mock('../../containers/use_delete_cases');
jest.mock('../../containers/use_get_cases');
jest.mock('../../containers/use_get_cases_status');

const useKibanaMock = useKibana as jest.Mock;
const useKibanaMock = useKibana as jest.Mocked<typeof useKibana>;
Copy link
Contributor

Choose a reason for hiding this comment

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

👍 Nice!

useUpdateCasesMock.mockReturnValue(defaultUpdateCases);
useGetCasesMock.mockReturnValue(defaultGetCases);
useDeleteCasesMock.mockReturnValue(defaultDeleteCases);
useGetCasesStatusMock.mockReturnValue(defaultCasesStatus);
moment.tz.setDefault('UTC');
});

Copy link
Contributor

Choose a reason for hiding this comment

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

Within the codebase there's like a weird style issue between newlines and not newlines between blocks of it and describe. No changes requested, just odd when I see two different styles and almost want a linter rule to happen at some point to just make it one way.

@@ -17,7 +17,7 @@ import {
kibanaObservable,
createSecuritySolutionStorageMock,
} from '../../mock';
import { createUseUiSetting$Mock } from '../../mock/kibana_react';
import { createUseUiSetting$Mock } from '../../lib/kibana/kibana_react.mock';
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice cleanup! Love how the mock files pattern is paying off more and more and how it gets easier to see where we can remove ours for theirs and reduce code in the code base.

return defaultValue;
}

throw new Error(`Unexpected config key: ${key}`);
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: These are usually throw new TypeError?
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypeError

No worries as I sometimes do this too here, just pointing it out as the better type.

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 catch, I'll change this to make it as accurate a mock as possible. I don't like having this mock, but as mentioned elsewhere there are a bunch of unit tests that are currently relying on "real" UI settings values, so removing it will take a little effort.


import { UseListsConfigReturn } from './use_lists_config';

export const getUseListsConfigMock: () => jest.Mocked<UseListsConfigReturn> = () => ({
Copy link
Contributor

Choose a reason for hiding this comment

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

👍 jest.Mocked<UseListsConfigReturn>

Copy link
Contributor

@FrankHassanabad FrankHassanabad left a comment

Choose a reason for hiding this comment

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

Code clean up and removal and fixing of types! Who could ask for more on a fresh Monday morning.

LGTM!

Throw an error of the same type if an unexpected key is used.
@kibanamachine
Copy link
Contributor

💚 Build Succeeded

Build metrics

async chunks size

id value diff baseline
securitySolution 7.2MB +36.0B 7.2MB

History

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

@rylnd rylnd merged commit 0758df8 into elastic:master Aug 24, 2020
rylnd added a commit to rylnd/kibana that referenced this pull request Aug 24, 2020
* Simplify our kibana mocks

* Simpler mock factory that returns an object instead of a thunk
  * We can use mockReturnValue instead of mockImplementation to
  accomplish the same
  * Allows us to replace createStartServices mock
* Uses unknown instead of any for mocks

* Clean up our manual use of kibana mocks in tests

* Since our useKibana mock returns a consistent mock, we can modify its
return value instead of re-mocking the entire thing
* Removes unnecessary uses of clearing/resetting mocks
  * If your mocks are configured at the beginning of each test this is
  usually unnecessary.
  * I left one case of clearAllMocks in all_cases/index.test since it
  defined several mock functions that were persistent across tests, and
  it was easier than moving their definitions to a beforeEach
* Removes some unnecessary overrides that seemed due to storage
previously not being mocked

* Rename some old occurrences of SIEM

* Cross-reference similar hooks via JSDoc

There's a good chance that the consumer might want the OTHER hook, so
let's make that discoverable.

* Adds jest tests for our useListsConfig hook

* adds mocks for the hooks upon which it depends

* Add a mock for our useListsConfig hook

Leverages this mock factory in our manual mock for this hook.

* Remove unneeded eslint exception

* Move kibana_react mocks into their own .mock file

We're trying to consolidate mocks to this pattern so that they're easier
to find and reuse.

* Remove intermediate mock factory

This was only being consumed by our general createStartServicesMock.

* Replace security_solution's alias for a core mock

This is just noise/tech debt, we should use the core mock directly when
we can.

* Remove unnecessary wrapper around core mocks

Instead let's just reference the core mocks themselves.

* Remove outdated references from upstream

* More accurate mock

Throw an error of the same type if an unexpected key is used.

Co-authored-by: Elastic Machine <[email protected]>
@rylnd rylnd deleted the mocks_cleanup branch August 24, 2020 20:51
rylnd added a commit that referenced this pull request Aug 25, 2020
)

* Simplify our kibana mocks

* Simpler mock factory that returns an object instead of a thunk
  * We can use mockReturnValue instead of mockImplementation to
  accomplish the same
  * Allows us to replace createStartServices mock
* Uses unknown instead of any for mocks

* Clean up our manual use of kibana mocks in tests

* Since our useKibana mock returns a consistent mock, we can modify its
return value instead of re-mocking the entire thing
* Removes unnecessary uses of clearing/resetting mocks
  * If your mocks are configured at the beginning of each test this is
  usually unnecessary.
  * I left one case of clearAllMocks in all_cases/index.test since it
  defined several mock functions that were persistent across tests, and
  it was easier than moving their definitions to a beforeEach
* Removes some unnecessary overrides that seemed due to storage
previously not being mocked

* Rename some old occurrences of SIEM

* Cross-reference similar hooks via JSDoc

There's a good chance that the consumer might want the OTHER hook, so
let's make that discoverable.

* Adds jest tests for our useListsConfig hook

* adds mocks for the hooks upon which it depends

* Add a mock for our useListsConfig hook

Leverages this mock factory in our manual mock for this hook.

* Remove unneeded eslint exception

* Move kibana_react mocks into their own .mock file

We're trying to consolidate mocks to this pattern so that they're easier
to find and reuse.

* Remove intermediate mock factory

This was only being consumed by our general createStartServicesMock.

* Replace security_solution's alias for a core mock

This is just noise/tech debt, we should use the core mock directly when
we can.

* Remove unnecessary wrapper around core mocks

Instead let's just reference the core mocks themselves.

* Remove outdated references from upstream

* More accurate mock

Throw an error of the same type if an unexpected key is used.

Co-authored-by: Elastic Machine <[email protected]>

Co-authored-by: Elastic Machine <[email protected]>
@MindyRS MindyRS added the Team: SecuritySolution Security Solutions Team working on SIEM, Endpoint, Timeline, Resolver, etc. label Sep 23, 2021
@elasticmachine
Copy link
Contributor

Pinging @elastic/security-solution (Team: SecuritySolution)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
chore release_note:skip Skip the PR/issue when compiling release notes Team: SecuritySolution Security Solutions Team working on SIEM, Endpoint, Timeline, Resolver, etc. Team:SIEM v7.10.0 v8.0.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants