-
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
Merged
pgayvallet
merged 7 commits into
elastic:main
from
pgayvallet:kbn-117255-apm-agent-jest-leak
Dec 7, 2021
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
616eba4
mock `elastic-apm-node` in `@kbn/test` jest preset
pgayvallet ec81523
adapt kbn-apm-config-loader tests
pgayvallet 05d9353
Merge branch 'main' into kbn-117255-apm-agent-jest-leak
kibanamachine 5ed187b
Merge branch 'main' into kbn-117255-apm-agent-jest-leak
kibanamachine df6e05d
use TS for agent mock
pgayvallet 42d31ed
Merge remote-tracking branch 'upstream/main' into kbn-117255-apm-agen…
pgayvallet 102dd3a
Merge branch 'main' into kbn-117255-apm-agent-jest-leak
kibanamachine File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
/** | ||
* `elastic-apm-node` patches the runtime at import time | ||
* causing memory leak with jest module sandbox, so it | ||
* needs to be mocked for tests | ||
*/ | ||
|
||
/* eslint-disable no-undef */ | ||
const agent = { | ||
start: jest.fn().mockImplementation(() => agent), | ||
isStarted: jest.fn().mockReturnValue(false), | ||
getServiceName: jest.fn().mockReturnValue('mock-service'), | ||
setFramework: jest.fn(), | ||
addPatch: jest.fn(), | ||
removePatch: jest.fn(), | ||
clearPatches: jest.fn(), | ||
lambda: jest.fn(), | ||
handleUncaughtExceptions: jest.fn(), | ||
captureError: jest.fn(), | ||
currentTraceparent: null, | ||
currentTraceIds: {}, | ||
startTransaction: jest.fn().mockReturnValue(null), | ||
setTransactionName: jest.fn(), | ||
endTransaction: jest.fn(), | ||
currentTransaction: null, | ||
startSpan: jest.fn(), | ||
currentSpan: null, | ||
setLabel: jest.fn().mockReturnValue(false), | ||
addLabels: jest.fn().mockReturnValue(false), | ||
setUserContext: jest.fn(), | ||
setCustomContext: jest.fn(), | ||
addFilter: jest.fn(), | ||
addErrorFilter: jest.fn(), | ||
addSpanFilter: jest.fn(), | ||
addTransactionFilter: jest.fn(), | ||
addMetadataFilter: jest.fn(), | ||
flush: jest.fn(), | ||
destroy: jest.fn(), | ||
registerMetric: jest.fn(), | ||
setTransactionOutcome: jest.fn(), | ||
setSpanOutcome: jest.fn(), | ||
middleware: { | ||
connect: jest.fn().mockReturnValue(jest.fn()), | ||
}, | ||
logger: { | ||
fatal: jest.fn(), | ||
error: jest.fn(), | ||
warn: jest.fn(), | ||
info: jest.fn(), | ||
debug: jest.fn(), | ||
trace: jest.fn(), | ||
}, | ||
}; | ||
|
||
module.exports = agent; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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: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)jest
is undeclared (file is not considered as a test file)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
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.
Maybe the Ops team can help with it?
elastic-apm-node
is not declared as aTYPES_DEPS
dependency inhttps://github.com/elastic/kibana/blob/e527c3fe297d3e17c6d5a8a24b4ea58a676fc641/packages/kbn-test/BUILD.bazel
jest
types are declared for all*.ts
fileskibana/packages/kbn-test/tsconfig.json
Line 12 in e527c3f
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.
My bad, it's actually only with
.js
extension. It worked fine with.ts
files.Thanks, that was just it.
Done in df6e05d