Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Alerting] Fixed Jest test suites with unhandled promise rejections #113213

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions x-pack/plugins/stack_alerts/server/feature.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,20 @@ import { featuresPluginMock } from '../../features/server/mocks';
import { BUILT_IN_ALERTS_FEATURE } from './feature';

describe('Stack Alerts Feature Privileges', () => {
test('feature privilege should contain all built-in rule types', async () => {
test('feature privilege should contain all built-in rule types', () => {
const context = coreMock.createPluginInitializerContext();
const plugin = new AlertingBuiltinsPlugin(context);
const coreSetup = coreMock.createSetup();
coreSetup.getStartServices = jest.fn().mockResolvedValue([
{
application: {},
},
{ triggersActionsUi: {} },
]);

const alertingSetup = alertsMock.createSetup();
const featuresSetup = featuresPluginMock.createSetup();
await plugin.setup(coreSetup, { alerting: alertingSetup, features: featuresSetup });
plugin.setup(coreSetup, { alerting: alertingSetup, features: featuresSetup });

const typesInFeaturePrivilege = BUILT_IN_ALERTS_FEATURE.alerting ?? [];
const typesInFeaturePrivilegeAll =
Expand Down
10 changes: 8 additions & 2 deletions x-pack/plugins/stack_alerts/server/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,18 @@ describe('AlertingBuiltins Plugin', () => {
context = coreMock.createPluginInitializerContext();
plugin = new AlertingBuiltinsPlugin(context);
coreSetup = coreMock.createSetup();
coreSetup.getStartServices = jest.fn().mockResolvedValue([
{
application: {},
},
{ triggersActionsUi: {} },
]);
});

it('should register built-in alert types', async () => {
it('should register built-in alert types', () => {
const alertingSetup = alertsMock.createSetup();
const featuresSetup = featuresPluginMock.createSetup();
await plugin.setup(coreSetup, { alerting: alertingSetup, features: featuresSetup });
plugin.setup(coreSetup, { alerting: alertingSetup, features: featuresSetup });

expect(alertingSetup.registerType).toHaveBeenCalledTimes(3);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,26 @@
*/

import * as React from 'react';
import { mountWithIntl } from '@kbn/test/jest';
import { mountWithIntl, nextTick } from '@kbn/test/jest';
import { act } from 'react-dom/test-utils';
import ConnectorAddModal from './connector_add_modal';
import { actionTypeRegistryMock } from '../../action_type_registry.mock';
import { ActionType, ConnectorValidationResult, GenericValidationResult } from '../../../types';
import { useKibana } from '../../../common/lib/kibana';
import { coreMock } from '../../../../../../../src/core/public/mocks';

jest.mock('../../../common/lib/kibana');
const mocks = coreMock.createSetup();
const actionTypeRegistry = actionTypeRegistryMock.create();
const useKibanaMock = useKibana as jest.Mocked<typeof useKibana>;

describe('connector_add_modal', () => {
beforeAll(async () => {
const mockes = coreMock.createSetup();
const [
{
application: { capabilities },
},
] = await mocks.getStartServices();
] = await mockes.getStartServices();
useKibanaMock().services.application.capabilities = {
...capabilities,
actions: {
Expand All @@ -34,21 +35,22 @@ describe('connector_add_modal', () => {
},
};
});
it('renders connector modal form if addModalVisible is true', () => {

it('renders connector modal form if addModalVisible is true', async () => {
const actionTypeModel = actionTypeRegistryMock.createMockActionTypeModel({
id: 'my-action-type',
iconClass: 'test',
selectMessage: 'test',
validateConnector: (): Promise<ConnectorValidationResult<unknown, unknown>> => {
return Promise.resolve({});
return Promise.resolve({ config: { errors: {} }, secrets: { errors: {} } });
},
validateParams: (): Promise<GenericValidationResult<unknown>> => {
const validationResult = { errors: {} };
return Promise.resolve(validationResult);
},
actionConnectorFields: null,
});
actionTypeRegistry.get.mockReturnValueOnce(actionTypeModel);
actionTypeRegistry.get.mockReturnValue(actionTypeModel);
actionTypeRegistry.has.mockReturnValue(true);

const actionType: ActionType = {
Expand All @@ -67,6 +69,11 @@ describe('connector_add_modal', () => {
actionTypeRegistry={actionTypeRegistry}
/>
);

await act(async () => {
await nextTick();
wrapper.update();
});
expect(wrapper.exists('.euiModalHeader')).toBeTruthy();
expect(wrapper.exists('[data-test-subj="saveActionButtonModal"]')).toBeTruthy();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import type { estypes } from '@elastic/elasticsearch';
import { loggingSystemMock } from '../../../../../../src/core/server/mocks';
import { Logger } from '../../../../../../src/core/server';
import {
TimeSeriesQueryParameters,
TimeSeriesQuery,
Expand All @@ -34,22 +35,22 @@ const DefaultQueryParams: TimeSeriesQuery = {
};

describe('timeSeriesQuery', () => {
let params: TimeSeriesQueryParameters;
const esClient = elasticsearchClientMock.createClusterClient().asScoped().asCurrentUser;

beforeEach(async () => {
params = {
logger: loggingSystemMock.create().get(),
esClient,
query: DefaultQueryParams,
};
});
const logger = loggingSystemMock.create().get() as jest.Mocked<Logger>;
const params = {
logger,
esClient,
query: DefaultQueryParams,
};

it('fails as expected when the callCluster call fails', async () => {
esClient.search = jest.fn().mockRejectedValue(new Error('woopsie'));
expect(timeSeriesQuery(params)).rejects.toThrowErrorMatchingInlineSnapshot(
`"error running search"`
);
await timeSeriesQuery(params);
expect(logger.warn.mock.calls[0]).toMatchInlineSnapshot(`
Array [
"indexThreshold timeSeriesQuery: callCluster error: woopsie",
]
`);
});

it('fails as expected when the query params are invalid', async () => {
Expand Down