-
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
Improve jest mocking infrastructure #38760
Conversation
Pinging @elastic/kibana-operations |
💚 Build Succeeded |
I think having the input from @elastic/kibana-platform team here would be a good idea, but in my opinion I think that instead of auto mocking things we should abstract the mock in a file and then import it where we need it. That way is easier to understand what is going on the tests without affecting the entire test sctructure. |
@mistic I synced with Spencer a lot about that issue, and for now our idea was that we're mocking away the very fundamental modules everyone would need to mock ( Those provided mocks themselves should nevertheless be very stupid and not have any logic (so not how the chrome mock in x-pack currently has). Instead if you need that specific logic you can just import those modules in your tests and modify the mock implementation. Also we currently have unnecessarily different mocks for x-pack than for OSS, which we should neither have ideally, but have shared mocks (the same as more shared configs in the long run). I am happy to take that into a larger round of discussion, but since I know this is at the moment blocking at least 2 other PRs I really want to proceed fast on this. |
Yeah after a small meeting about that it was more clear the problems that route us to that possible solution. While I think we could manually import predefined mocks for chrome and metadata where we need it because that would simplify problems, I think we can also consider to auto mock those specific dependencies in case that help us somehow. |
💚 Build Succeeded |
This comment has been minimized.
This comment has been minimized.
Jenkins, test this - CI outage so I cancelled |
@@ -3,6 +3,7 @@ module.exports = { | |||
{ | |||
files: [ | |||
'**/*.{test,test.mocks,mock}.{js,ts,tsx}', | |||
'**/__mocks__/**/*.{js,ts,tsx}', |
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.
ℹ️ This way we make eslint aware of jest
in all __mocks__
directories.
@@ -36,6 +36,7 @@ const findSourceFiles = async (patterns, cwd = fromRoot('.')) => { | |||
'**/_*.js', | |||
'**/*.test.js', | |||
'**/*.test.mocks.js', | |||
'**/__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.
ℹ️ Without that line __mocks__
files (which could contain calls to jest
) would be bundled for Karma/Mocha tests and then cause them to fail, since jest
is not available there.
💚 Build Succeeded |
💚 Build Succeeded |
I like the simplicity of automatically loading the correct mock with Auto-mocking things like I don't know if this is true in every case, but taking LanguageSwitcher as an example, I believe I can imagine that our legacy angular code hides many dependencies in subtle ways making refactoring like this to fix the problem hard. But perhaps having to sprinkle |
@skaapgif I partly agree with you, why the suggestion was only to automatically enable absolute fundamental mocks. Also after discussing with Spencer yesterday we agreed to remove the auto mocking of Even given our past sins, I am not sure if it's a good thing to force that onto every (unrelated) user, maybe even that user that wrote their code 100% perfect and mockable and so on. I would be (and thus the very long description in the file) very very careful what to automock and always have this close to nothing. But |
Yeah I agree that there's a balance between "paying for your sins" and every developer being punished by bad practises they have no control over. |
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 tiny change, otherwise LGTM
src/dev/jest/setup/mocks.js
Outdated
* The mocks that are enabled that way live inside the `__mocks__` folders beside their implementation files. | ||
*/ | ||
|
||
jest.mock('ui/kfetch'); |
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.
Let’s remove this, kfetch feels like something that should be under control of the units being tested.
💔 Build Failed |
Jenkins, Test this - failure to download ES |
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 on green
💔 Build Failed |
Jenkins, test this - artifact download error again :( |
💚 Build Succeeded |
* Always mock metadata/chrome in OSS * Enable jest env in jest mocks * Exclude jest mocks in karma bundles * Use setupFilesAfterEnv in config * Remove chrome/metadata mock from x-pack * Remove kuery mock * Add missing mock to SIEM test * Fix typo in mock import * Remove top level capabilities x-pack mock * Move kfetch mock to ui/public * Move moment-timezone to mocks file * Unmock kfetch in kfetch specific tests * Make kfetch mock manual * Removed unnecessary jest.mock * Remove kfetch unmocks
* Always mock metadata/chrome in OSS * Enable jest env in jest mocks * Exclude jest mocks in karma bundles * Use setupFilesAfterEnv in config * Remove chrome/metadata mock from x-pack * Remove kuery mock * Add missing mock to SIEM test * Fix typo in mock import * Remove top level capabilities x-pack mock * Move kfetch mock to ui/public * Move moment-timezone to mocks file * Unmock kfetch in kfetch specific tests * Make kfetch mock manual * Removed unnecessary jest.mock * Remove kfetch unmocks
Summary
This PR improves and aligns the jest mock infrastructure in the following way:
__mocks__
files (jest mocks) are no longer bundled inside the Karma/Mocha test bundles.jest
is the environment in__mocks__
files.__mocks__
folder inx-pack/plugins
anymore that mocks away modules fromui/public
.x-pack
should not provide any top level__mocks__
forui/public
modules anymore. Mocks should either live locally inside test suites (or extracted "locally" into files, like I did in the spaces plugin). Generic mocks should live inside a__mocks__
folder beside their implementation. Everyone can then simply enabling those mocks by calling e.g.jest.mock('ui/kfetch')
and Jest will automatically find the appropriate mock file beside the implementation file.We also enable mocks for a couple of very very basic modules by default in all Jest tests that would otherwise fail a lot of test. These modules are currently:
ui/chrome
andui/metadata
.You might now see an error message like:
when running jest tests. This is a jest bug, that's tracked in jestjs/jest#2070
I mainly touched two test suites and moved mocks into those, so I would appreciate a quick glance by the @elastic/kibana-security team and the @elastic/secops team for their test suites.
Checklist
Use
strikethroughsto remove checklist items you don't feel are applicable to this PR.[ ] This was checked for cross-browser compatibility, including a check against IE11[ ] Any text added follows EUI's writing guidelines, uses sentence case text and includes i18n support[ ] Documentation was added for features that require explanation or tutorials[ ] This was checked for keyboard-only and screenreader accessibilityFor maintainers
[ ] This was checked for breaking API changes and was labeled appropriately[ ] This includes a feature addition or change that requires a release note and was labeled appropriately