Skip to content

Commit

Permalink
mock elastic-apm-node in @kbn/test jest preset
Browse files Browse the repository at this point in the history
  • Loading branch information
pgayvallet committed Dec 3, 2021
1 parent f3aa761 commit 616eba4
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/kbn-test/jest-preset.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ module.exports = {
moduleNameMapper: {
'@elastic/eui/lib/(.*)?': '<rootDir>/node_modules/@elastic/eui/test-env/$1',
'@elastic/eui$': '<rootDir>/node_modules/@elastic/eui/test-env',
'elastic-apm-node': '<rootDir>/node_modules/@kbn/test/target_node/jest/mocks/apm_agent_mock.js',
'\\.module.(css|scss)$':
'<rootDir>/node_modules/@kbn/test/target_node/jest/mocks/css_module_mock.js',
'\\.(css|less|scss)$': '<rootDir>/node_modules/@kbn/test/target_node/jest/mocks/style_mock.js',
Expand Down
62 changes: 62 additions & 0 deletions packages/kbn-test/src/jest/mocks/apm_agent_mock.js
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;

0 comments on commit 616eba4

Please sign in to comment.