-
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
[Security Solution][Detections] Cleaning up mocks/tests #74920
Conversation
* 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.
@@ -97,23 +96,16 @@ describe('AllCases', () => { | |||
}); | |||
/* eslint-enable no-console */ | |||
beforeEach(() => { | |||
jest.resetAllMocks(); | |||
jest.clearAllMocks(); |
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 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() } }}> |
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 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>( |
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 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.
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.
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.
* 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.
Pinging @elastic/siem (Team:SIEM) |
@elasticmachine merge upstream |
@elasticmachine merge upstream |
@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>; |
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.
👍 Nice!
useUpdateCasesMock.mockReturnValue(defaultUpdateCases); | ||
useGetCasesMock.mockReturnValue(defaultGetCases); | ||
useDeleteCasesMock.mockReturnValue(defaultDeleteCases); | ||
useGetCasesStatusMock.mockReturnValue(defaultCasesStatus); | ||
moment.tz.setDefault('UTC'); | ||
}); | ||
|
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.
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'; |
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.
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}`); |
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.
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.
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 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> = () => ({ |
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.
👍 jest.Mocked<UseListsConfigReturn>
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.
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.
💚 Build SucceededBuild metricsasync chunks size
History
To update your PR or re-run it, just comment with: |
* 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]>
) * 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]>
Pinging @elastic/security-solution (Team: SecuritySolution) |
Summary
This is a chore PR that's a followup to #74582 and #75378 . Contents:
useListsConfig
hook.mock
files in files/modules touchedany
withunknown
kibana_core
mocks with mocks provided by core (which were just delegating directly to anyway)clearAll
, etc.Checklist
Delete any items that are not applicable to this PR.
For maintainers