-
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
mock elastic-apm-node
in @kbn/test
jest preset
#120324
mock elastic-apm-node
in @kbn/test
jest preset
#120324
Conversation
Pinging @elastic/kibana-core (Team:Core) |
Pinging @elastic/kibana-operations (Team:Operations) |
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
@@ -0,0 +1,62 @@ | |||
/* |
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: Should we migrate this file to TS? I guess we don't want to, so we don't need to import the original library to match the types (and add the dependency). Is that right?
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 started with TS (which ensured I have a full mock as I used a jest.Mock<Agent>
as the type of this structure when coding it), but:
- You can't use
module.export =
, and not 100% sureexport default
is exactly the same (plus I had to disable an eslint warning as default exports are not allowed here) - I was forced to disable some other eslint warnings because
jest
is undeclared (file is not considered as a test file) - I had an error while running
kbn bootstrap
because this module can'timport type { Agent } from "elastic-apm-node" for some reason
(probably related to bazel project dependencies)
so given that
- all other mocks in this folder are still in javascript
I assumed this was okay-ish
But I can retry using TS if we think this is preferable, @elastic/kibana-operations wdyt?
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 your points are very valid! I'm just happy that we have them documented now in this PR discussion 🙂
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 might be okay to start with the current implementation, but in the long term, we need TypeScript to enforce the mock compatibility with an interface of the original package.
You can't use module.export =, and not 100% sure export default is exactly the same (plus I had to disable an eslint warning as default exports are not allowed here)
Maybe the Ops team can help with it?
I had an error while running kbn bootstrap because this module can't import type { Agent } from "elastic-apm-node" for some reason (probably related to bazel project dependencies)
elastic-apm-node
is not declared as a TYPES_DEPS
dependency in
https://github.com/elastic/kibana/blob/e527c3fe297d3e17c6d5a8a24b4ea58a676fc641/packages/kbn-test/BUILD.bazel
I was forced to disable some other eslint warnings because jest is undeclared (file is not considered as a test file)
jest
types are declared for all *.ts
files
kibana/packages/kbn-test/tsconfig.json
Line 12 in e527c3f
"types": ["jest", "node"] |
all other mocks in this folder are still in javascript
They are quite small and don't mock such a large API surface.
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
types are declared for all *.ts files
My bad, it's actually only with .js
extension. It worked fine with .ts
files.
elastic-apm-node is not declared as a TYPES_DEPS dependency in
Thanks, that was just it.
Done in df6e05d
@elasticmachine merge upstream |
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.
Checked out this branch and then commented all of the security_solution
mocks:
// moduleNameMapper: {
// 'core/server$': '<rootDir>/x-pack/plugins/security_solution/server/__mocks__/core.mock.ts',
// 'task_manager/server$':
// '<rootDir>/x-pack/plugins/security_solution/server/__mocks__/task_manager.mock.ts',
// 'alerting/server$': '<rootDir>/x-pack/plugins/security_solution/server/__mocks__/alert.mock.ts',
// 'actions/server$': '<rootDir>/x-pack/plugins/security_solution/server/__mocks__/action.mock.ts',
// },
In the files:
modified: x-pack/plugins/security_solution/server/client/jest.config.js
modified: x-pack/plugins/security_solution/server/endpoint/jest.config.js
modified: x-pack/plugins/security_solution/server/fleet_integration/jest.config.js
modified: x-pack/plugins/security_solution/server/jest.config.js
modified: x-pack/plugins/security_solution/server/lib/jest.config.js
modified: x-pack/plugins/security_solution/server/search_strategy/jest.config.js
modified: x-pack/plugins/security_solution/server/usage/jest.config.js
modified: x-pack/plugins/security_solution/server/utils/jest.config.js
And I was able to run under 1 gig like so:
node --max-old-space-size=1000 ./node_modules/.bin/jest --runInBand --logHeapUsage --no-cache --config x-pack/plugins/security_solution/jest.config.dev.js x-pack/plugins/security_solution/server
server side. So I think this fixes the worst if there are any others.
The heap snapshots at the end of the test run was 756 MB
:
PASS x-pack/plugins/security_solution/server/endpoint/routes/resolver/tree/utils/index.test.ts (756 MB heap size)
so I probably could have run the tests with less memory but I choose just 1 gig as that's what we have been doing previously.
@elasticmachine merge upstream |
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
@elasticmachine merge upstream |
💚 Build Succeeded
Metrics [docs]
History
To update your PR or re-run it, just comment with: |
* mock `elastic-apm-node` in `@kbn/test` jest preset * adapt kbn-apm-config-loader tests * use TS for agent mock Co-authored-by: Kibana Machine <[email protected]>
* mock `elastic-apm-node` in `@kbn/test` jest preset * adapt kbn-apm-config-loader tests * use TS for agent mock Co-authored-by: Kibana Machine <[email protected]> Co-authored-by: Kibana Machine <[email protected]>
…-chromium-before-compiling-pdf * 'main' of github.com:elastic/kibana: (121 commits) FTR should use the new kibana_system user (elastic#120436) [Lens] Temporarily exclude Mosaic/Waffle from the suggestions list (elastic#120488) [Reporting] Fix slow CSV with large max size bytes (elastic#120365) [CTI] Threat Intel Card on Overview page needs to accommodate Fleet TI integrations - (elastic#120459) [Dashboard] Added KibanaThemeProvider. (elastic#120122) add more rule_registry unit tests (elastic#120323) [Lens] update xpack.lens.pie.smallValuesWarningMessage text (elastic#120478) [Fleet] Simplified package policy create API, enriching default values (elastic#119739) mock `elastic-apm-node` in `@kbn/test` jest preset (elastic#120324) [RAC] Rename occurrences of alert_type to rule_type in Infra (elastic#120455) [Security Solutions] Removes tech debt of exporting all from linter rule for timeline plugin (elastic#120437) [Workplace Search] Add API Keys view to replace Access tokens (elastic#120147) [Security Solutions] Removes tech debt of exporting all from linter rule for cases plugin in the server section (elastic#120411) Add support for threat.feed.name (elastic#120250) [Rule Registry] Rewrite APM registry rules for Observability (elastic#117740) [docs] Fix artifact layout formatting (elastic#119386) [Workplace Search] Add a technical preview of connecting GitHub via GitHub apps (elastic#119764) add osquery notes for 7.16 (elastic#120407) chore(NA): splits types from code on @kbn/docs-utils (elastic#120533) [Reporting] Decouple screenshotting plugin from the reporting (elastic#120110) ... # Conflicts: # x-pack/plugins/reporting/server/browsers/chromium/driver_factory/index.test.ts # x-pack/plugins/reporting/server/browsers/chromium/driver_factory/index.ts # x-pack/plugins/reporting/server/lib/screenshots/observable.test.ts # x-pack/plugins/reporting/server/lib/screenshots/observable.ts # x-pack/plugins/reporting/server/test_helpers/create_mock_browserdriverfactory.ts
* mock `elastic-apm-node` in `@kbn/test` jest preset * adapt kbn-apm-config-loader tests * use TS for agent mock Co-authored-by: Kibana Machine <[email protected]>
Summary
Fix #117255
In #117255 we investigated a memory leak in jest environment, that lead to
elastic-apm-node
patching native modules at runtime (please refer to the issue for more details).This PR fixes it by mocking
elastic-apm-node
in our@kbn/test
presetThanks to @FrankHassanabad and @academo for providing the scripts helping identifying the leak and starting the investigation.
How to test
You need to run tests that are identified as leaking using
--runInBand
, and confirm that no memory leaks with this PR's branch.I created a
x-pack/plugins/alerting/server/sample_test.ts
file with the following test:and used the following script (credits to @FrankHassanabad):
Using with