Skip to content

Commit

Permalink
Fix Jest tests
Browse files Browse the repository at this point in the history
  • Loading branch information
miltonhultgren committed Jun 3, 2022
1 parent 6c8bb41 commit aaa137c
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 21 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 21 additions & 17 deletions x-pack/plugins/monitoring/server/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,6 @@
import fs from 'fs';
import { configSchema, createConfig } from './config';

const MOCKED_PATHS = [
'/proc/self/cgroup',
'packages/kbn-dev-utils/certs/ca.crt',
'packages/kbn-dev-utils/certs/elasticsearch.crt',
'packages/kbn-dev-utils/certs/elasticsearch.key',
];

beforeEach(() => {
jest.spyOn(fs, 'readFileSync').mockImplementation((path, enc) => {
if (typeof path === 'string' && MOCKED_PATHS.includes(path) && enc === 'utf8') {
return `contents-of-${path}`;
}

throw new Error(`unpexpected arguments to fs.readFileSync: ${path}, ${enc}`);
});
});

describe('config schema', () => {
it('generates proper defaults', () => {
expect(configSchema.validate({})).toMatchInlineSnapshot(`
Expand Down Expand Up @@ -99,6 +82,11 @@ describe('config schema', () => {
},
},
"enabled": true,
"kibana": Object {
"reporting": Object {
"stale_status_threshold_seconds": 120,
},
},
"logs": Object {
"index": "filebeat-*",
},
Expand All @@ -115,6 +103,22 @@ describe('config schema', () => {
});

describe('createConfig()', () => {
const MOCKED_PATHS = [
'packages/kbn-dev-utils/certs/ca.crt',
'packages/kbn-dev-utils/certs/elasticsearch.crt',
'packages/kbn-dev-utils/certs/elasticsearch.key',
];

beforeEach(() => {
jest.spyOn(fs, 'readFileSync').mockImplementation((path, enc) => {
if (typeof path === 'string' && MOCKED_PATHS.includes(path) && enc === 'utf8') {
return `contents-of-${path}`;
}

throw new Error(`unpexpected arguments to fs.readFileSync: ${path}, ${enc}`);
});
});

it('should wrap in Elasticsearch config', async () => {
const config = createConfig(
configSchema.validate({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,22 @@
import moment from 'moment';
import { handleResponse } from './get_kibana_info';

jest.mock('../../static_globals', () => ({
Globals: {
app: {
config: {
ui: {
kibana: {
reporting: {
stale_status_threshold_seconds: 120,
},
},
},
},
},
},
}));

describe('get_kibana_info', () => {
// TODO: test was not running before and is not up to date
it.skip('return undefined for empty response', () => {
Expand All @@ -16,13 +32,14 @@ describe('get_kibana_info', () => {
});

it('return mapped data for result with hits, availability = true', () => {
const timestamp = moment().format();
const result = handleResponse({
hits: {
hits: [
{
_source: {
kibana_stats: {
timestamp: moment().format(),
timestamp,
kibana: {
data: 123,
},
Expand All @@ -31,27 +48,33 @@ describe('get_kibana_info', () => {
free_in_bytes: 123000,
},
},
process: {
uptime_in_millis: 3000,
},
},
},
},
],
},
});
expect(result).toEqual({
availability: true,
lastSeenTimestamp: timestamp,
statusIsStale: false,
data: 123,
os_memory_free: 123000,
uptime: 3000,
});
});

it('return mapped data for result with hits, availability = false', () => {
const timestamp = moment().subtract(11, 'minutes').format();
const result = handleResponse({
hits: {
hits: [
{
_source: {
kibana_stats: {
timestamp: moment().subtract(11, 'minutes').format(),
timestamp,
kibana: {
data: 123,
},
Expand All @@ -60,16 +83,21 @@ describe('get_kibana_info', () => {
free_in_bytes: 123000,
},
},
process: {
uptime_in_millis: 3000,
},
},
},
},
],
},
});
expect(result).toEqual({
availability: false,
lastSeenTimestamp: timestamp,
statusIsStale: true,
data: 123,
os_memory_free: 123000,
uptime: 3000,
});
});
});

0 comments on commit aaa137c

Please sign in to comment.