|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0 and the Server Side Public License, v 1; you may not use this file except |
| 5 | + * in compliance with, at your election, the Elastic License 2.0 or the Server |
| 6 | + * Side Public License, v 1. |
| 7 | + */ |
| 8 | + |
| 9 | +import { mockLoadConfiguration } from './init_apm.test.mocks'; |
| 10 | + |
| 11 | +import { initApm } from './init_apm'; |
| 12 | +import apm from 'elastic-apm-node'; |
| 13 | + |
| 14 | +describe('initApm', () => { |
| 15 | + let apmAddFilterSpy: jest.SpyInstance; |
| 16 | + let apmStartSpy: jest.SpyInstance; |
| 17 | + let getConfig: jest.Mock; |
| 18 | + |
| 19 | + beforeEach(() => { |
| 20 | + apmAddFilterSpy = jest.spyOn(apm, 'addFilter').mockImplementation(() => undefined); |
| 21 | + apmStartSpy = jest.spyOn(apm, 'start').mockImplementation(() => undefined as any); |
| 22 | + getConfig = jest.fn(); |
| 23 | + |
| 24 | + mockLoadConfiguration.mockImplementation(() => ({ |
| 25 | + getConfig, |
| 26 | + })); |
| 27 | + }); |
| 28 | + |
| 29 | + afterEach(() => { |
| 30 | + jest.restoreAllMocks(); |
| 31 | + mockLoadConfiguration.mockReset(); |
| 32 | + }); |
| 33 | + |
| 34 | + it('calls `loadConfiguration` with the correct options', () => { |
| 35 | + initApm(['foo', 'bar'], 'rootDir', true, 'service-name'); |
| 36 | + |
| 37 | + expect(mockLoadConfiguration).toHaveBeenCalledTimes(1); |
| 38 | + expect(mockLoadConfiguration).toHaveBeenCalledWith(['foo', 'bar'], 'rootDir', true); |
| 39 | + }); |
| 40 | + |
| 41 | + it('calls `apmConfigLoader.getConfig` with the correct options', () => { |
| 42 | + initApm(['foo', 'bar'], 'rootDir', true, 'service-name'); |
| 43 | + |
| 44 | + expect(getConfig).toHaveBeenCalledTimes(1); |
| 45 | + expect(getConfig).toHaveBeenCalledWith('service-name'); |
| 46 | + }); |
| 47 | + |
| 48 | + it('registers a filter using `addFilter`', () => { |
| 49 | + initApm(['foo', 'bar'], 'rootDir', true, 'service-name'); |
| 50 | + |
| 51 | + expect(apmAddFilterSpy).toHaveBeenCalledTimes(1); |
| 52 | + expect(apmAddFilterSpy).toHaveBeenCalledWith(expect.any(Function)); |
| 53 | + }); |
| 54 | + |
| 55 | + it('starts apm with the config returned from `getConfig`', () => { |
| 56 | + const config = { |
| 57 | + foo: 'bar', |
| 58 | + }; |
| 59 | + getConfig.mockReturnValue(config); |
| 60 | + |
| 61 | + initApm(['foo', 'bar'], 'rootDir', true, 'service-name'); |
| 62 | + |
| 63 | + expect(apmStartSpy).toHaveBeenCalledTimes(1); |
| 64 | + expect(apmStartSpy).toHaveBeenCalledWith(config); |
| 65 | + }); |
| 66 | +}); |
0 commit comments