diff --git a/x-pack/legacy/plugins/uptime/server/lib/adapters/monitor_states/__tests__/elasticsearch_monitor_states_adapter.test.ts b/x-pack/legacy/plugins/uptime/server/lib/adapters/monitor_states/__tests__/elasticsearch_monitor_states_adapter.test.ts index 67a66362e4cc5..1a90699090b29 100644 --- a/x-pack/legacy/plugins/uptime/server/lib/adapters/monitor_states/__tests__/elasticsearch_monitor_states_adapter.test.ts +++ b/x-pack/legacy/plugins/uptime/server/lib/adapters/monitor_states/__tests__/elasticsearch_monitor_states_adapter.test.ts @@ -8,6 +8,8 @@ import { DatabaseAdapter } from '../../database'; import exampleFilter from './example_filter.json'; import monitorState from './monitor_states_docs.json'; import { ElasticsearchMonitorStatesAdapter } from '../elasticsearch_monitor_states_adapter'; +import { get, set } from 'lodash'; +import { assertCloseTo } from '../../../helper'; describe('ElasticsearchMonitorStatesAdapter', () => { let database: DatabaseAdapter; @@ -37,6 +39,7 @@ describe('ElasticsearchMonitorStatesAdapter', () => { }); it('applies an appropriate filter section to the query based on filters', async () => { + expect.assertions(3); const adapter = new ElasticsearchMonitorStatesAdapter(database); await adapter.legacyGetMonitorStates( {}, @@ -45,6 +48,28 @@ describe('ElasticsearchMonitorStatesAdapter', () => { JSON.stringify(exampleFilter), 'down' ); + expect(searchMock).toHaveBeenCalledTimes(3); + const fixedInterval = parseInt( + get( + searchMock.mock.calls[2][1], + 'body.aggs.by_id.aggs.histogram.date_histogram.fixed_interval', + '' + ).split('ms')[0], + 10 + ); + expect(fixedInterval).not.toBeNaN(); + /** + * This value can sometimes be off by 1 as a result of fuzzy calculation. + * + * It had no implications in practice, but from a test standpoint can cause flaky + * snapshot failures. + */ + assertCloseTo(fixedInterval, 36000, 100); + set( + searchMock.mock.calls[2][1], + 'body.aggs.by_id.aggs.histogram.date_histogram.fixed_interval', + '36000ms' + ); expect(searchMock.mock.calls).toMatchSnapshot(); }); }); diff --git a/x-pack/legacy/plugins/uptime/server/lib/adapters/monitors/__tests__/elasticsearch_monitors_adapter.test.ts b/x-pack/legacy/plugins/uptime/server/lib/adapters/monitors/__tests__/elasticsearch_monitors_adapter.test.ts index 9fce667adf613..b21fb982bfb3b 100644 --- a/x-pack/legacy/plugins/uptime/server/lib/adapters/monitors/__tests__/elasticsearch_monitors_adapter.test.ts +++ b/x-pack/legacy/plugins/uptime/server/lib/adapters/monitors/__tests__/elasticsearch_monitors_adapter.test.ts @@ -8,12 +8,7 @@ import { get, set } from 'lodash'; import { ElasticsearchMonitorsAdapter } from '../elasticsearch_monitors_adapter'; import { CountParams, CountResponse } from 'elasticsearch'; import mockChartsData from './monitor_charts_mock.json'; - -const assertCloseTo = (actual: number, expected: number, precision: number) => { - if (Math.abs(expected - actual) > precision) { - throw new Error(`expected [${actual}] to be within ${precision} of ${actual}`); - } -}; +import { assertCloseTo } from '../../../helper'; // FIXME: there are many untested functions in this adapter. They should be tested. describe('ElasticsearchMonitorsAdapter', () => { diff --git a/x-pack/legacy/plugins/uptime/server/lib/helper/__test__/__snapshots__/assert_close_to.test.ts.snap b/x-pack/legacy/plugins/uptime/server/lib/helper/__test__/__snapshots__/assert_close_to.test.ts.snap new file mode 100644 index 0000000000000..7dfb8c88be1ca --- /dev/null +++ b/x-pack/legacy/plugins/uptime/server/lib/helper/__test__/__snapshots__/assert_close_to.test.ts.snap @@ -0,0 +1,3 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`assertCloseTo throws an error when expected value is outside of precision range 1`] = `"expected [12500] to be within 100 of 10000"`; diff --git a/x-pack/legacy/plugins/uptime/server/lib/helper/__test__/assert_close_to.test.ts b/x-pack/legacy/plugins/uptime/server/lib/helper/__test__/assert_close_to.test.ts new file mode 100644 index 0000000000000..6ccedcf7153dc --- /dev/null +++ b/x-pack/legacy/plugins/uptime/server/lib/helper/__test__/assert_close_to.test.ts @@ -0,0 +1,21 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import { assertCloseTo } from '../assert_close_to'; + +describe('assertCloseTo', () => { + it('does not throw an error when expected value is correct', () => { + assertCloseTo(10000, 10001, 100); + }); + + it('does not throw an error when expected value is under actual, but within precision threshold', () => { + assertCloseTo(10000, 9875, 300); + }); + + it('throws an error when expected value is outside of precision range', () => { + expect(() => assertCloseTo(10000, 12500, 100)).toThrowErrorMatchingSnapshot(); + }); +}); diff --git a/x-pack/legacy/plugins/uptime/server/lib/helper/assert_close_to.ts b/x-pack/legacy/plugins/uptime/server/lib/helper/assert_close_to.ts new file mode 100644 index 0000000000000..13b6f3688809c --- /dev/null +++ b/x-pack/legacy/plugins/uptime/server/lib/helper/assert_close_to.ts @@ -0,0 +1,11 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +export const assertCloseTo = (actual: number, expected: number, precision: number) => { + if (Math.abs(expected - actual) > precision) { + throw new Error(`expected [${expected}] to be within ${precision} of ${actual}`); + } +}; diff --git a/x-pack/legacy/plugins/uptime/server/lib/helper/index.ts b/x-pack/legacy/plugins/uptime/server/lib/helper/index.ts index 19d5a3b00237d..a2a72825c6b98 100644 --- a/x-pack/legacy/plugins/uptime/server/lib/helper/index.ts +++ b/x-pack/legacy/plugins/uptime/server/lib/helper/index.ts @@ -9,3 +9,4 @@ export { formatEsBucketsForHistogram } from './format_es_buckets_for_histogram'; export { getFilterClause } from './get_filter_clause'; export { getHistogramInterval } from './get_histogram_interval'; export { parseFilterQuery } from './parse_filter_query'; +export { assertCloseTo } from './assert_close_to';