Skip to content

Commit

Permalink
mock elastic-apm-node in @kbn/test jest preset (elastic#120324)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
pgayvallet and kibanamachine committed Dec 7, 2021
1 parent dd0744d commit edbc21d
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 9 deletions.
19 changes: 10 additions & 9 deletions packages/kbn-apm-config-loader/src/init_apm.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ import { initApm } from './init_apm';
import apm from 'elastic-apm-node';

describe('initApm', () => {
let apmAddFilterSpy: jest.SpyInstance;
let apmStartSpy: jest.SpyInstance;
let apmAddFilterMock: jest.Mock;
let apmStartMock: jest.Mock;
let getConfig: jest.Mock;

beforeEach(() => {
apmAddFilterSpy = jest.spyOn(apm, 'addFilter').mockImplementation(() => undefined);
apmStartSpy = jest.spyOn(apm, 'start').mockImplementation(() => undefined as any);
apmAddFilterMock = apm.addFilter as jest.Mock;
apmStartMock = apm.start as jest.Mock;
getConfig = jest.fn();

mockLoadConfiguration.mockImplementation(() => ({
Expand All @@ -27,7 +27,8 @@ describe('initApm', () => {
});

afterEach(() => {
jest.restoreAllMocks();
apmAddFilterMock.mockReset();
apmStartMock.mockReset();
mockLoadConfiguration.mockReset();
});

Expand All @@ -48,8 +49,8 @@ describe('initApm', () => {
it('registers a filter using `addFilter`', () => {
initApm(['foo', 'bar'], 'rootDir', true, 'service-name');

expect(apmAddFilterSpy).toHaveBeenCalledTimes(1);
expect(apmAddFilterSpy).toHaveBeenCalledWith(expect.any(Function));
expect(apmAddFilterMock).toHaveBeenCalledTimes(1);
expect(apmAddFilterMock).toHaveBeenCalledWith(expect.any(Function));
});

it('starts apm with the config returned from `getConfig`', () => {
Expand All @@ -60,7 +61,7 @@ describe('initApm', () => {

initApm(['foo', 'bar'], 'rootDir', true, 'service-name');

expect(apmStartSpy).toHaveBeenCalledTimes(1);
expect(apmStartSpy).toHaveBeenCalledWith(config);
expect(apmStartMock).toHaveBeenCalledTimes(1);
expect(apmStartMock).toHaveBeenCalledWith(config);
});
});
1 change: 1 addition & 0 deletions packages/kbn-test/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ TYPES_DEPS = [
"//packages/kbn-i18n-react:npm_module_types",
"//packages/kbn-utils",
"@npm//@elastic/elasticsearch",
"@npm//elastic-apm-node",
"@npm//del",
"@npm//form-data",
"@npm//jest",
Expand Down
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
63 changes: 63 additions & 0 deletions packages/kbn-test/src/jest/mocks/apm_agent_mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* 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.
*/

import type { Agent } from 'elastic-apm-node';

/**
* `elastic-apm-node` patches the runtime at import time
* causing memory leak with jest module sandbox, so it
* needs to be mocked for tests
*/
const agent: jest.Mocked<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(),
},
};

// eslint-disable-next-line import/no-default-export
export default agent;

0 comments on commit edbc21d

Please sign in to comment.