Skip to content

Commit

Permalink
[Enterprise Search] Small test helper for expected async errors (#88422)
Browse files Browse the repository at this point in the history
* Add small helper to reduce boilerplate on async tests expected to throw

- Also hopefully a bit more descriptive on what's happening

* Update all test files using the previous try/catch do nothing blocks to use new helper
  • Loading branch information
Constance authored Jan 15, 2021
1 parent c0a833d commit d2de5d0
Show file tree
Hide file tree
Showing 12 changed files with 121 additions and 178 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* 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 expectedAsyncError = async (promise: Promise<unknown>) => {
let expectedError: unknown;

try {
await promise;
} catch (e) {
// Silence expected errors from being reported by tests

// Pass back expected error to optionally assert on
expectedError = e;
}

return expectedError;
};
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ export { mountWithIntl } from './mount_with_i18n.mock';
export { shallowWithIntl } from './shallow_with_i18n.mock';
export { rerender } from './enzyme_rerender.mock';
// Note: shallow_useeffect must be imported directly as a file

export { expectedAsyncError } from './expected_async_error';
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { LogicMounter } from '../../../__mocks__/kea.mock';
import { LogicMounter, expectedAsyncError } from '../../../__mocks__';

jest.mock('../../../shared/kibana', () => ({
KibanaLogic: { values: { history: { location: { search: '' } } } },
Expand Down Expand Up @@ -215,12 +215,8 @@ describe('AnalyticsLogic', () => {
mount();
jest.spyOn(AnalyticsLogic.actions, 'onAnalyticsUnavailable');

try {
AnalyticsLogic.actions.loadAnalyticsData();
await promise;
} catch {
// Do nothing
}
AnalyticsLogic.actions.loadAnalyticsData();
await expectedAsyncError(promise);

expect(flashAPIErrors).toHaveBeenCalledWith('error');
expect(AnalyticsLogic.actions.onAnalyticsUnavailable).toHaveBeenCalled();
Expand Down Expand Up @@ -293,12 +289,8 @@ describe('AnalyticsLogic', () => {
mount();
jest.spyOn(AnalyticsLogic.actions, 'onAnalyticsUnavailable');

try {
AnalyticsLogic.actions.loadQueryData('some-query');
await promise;
} catch {
// Do nothing
}
AnalyticsLogic.actions.loadQueryData('some-query');
await expectedAsyncError(promise);

expect(flashAPIErrors).toHaveBeenCalledWith('error');
expect(AnalyticsLogic.actions.onAnalyticsUnavailable).toHaveBeenCalled();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { LogicMounter } from '../../../__mocks__/kea.mock';
import { LogicMounter, mockHttpValues, expectedAsyncError } from '../../../__mocks__';

import { mockHttpValues } from '../../../__mocks__';
jest.mock('../../../shared/http', () => ({
HttpLogic: { values: mockHttpValues },
}));
Expand Down Expand Up @@ -1093,11 +1092,9 @@ describe('CredentialsLogic', () => {
http.get.mockReturnValue(promise);

CredentialsLogic.actions.fetchCredentials();
try {
await promise;
} catch {
expect(flashAPIErrors).toHaveBeenCalledWith('An error occured');
}
await expectedAsyncError(promise);

expect(flashAPIErrors).toHaveBeenCalledWith('An error occured');
});
});

Expand All @@ -1124,11 +1121,9 @@ describe('CredentialsLogic', () => {
http.get.mockReturnValue(promise);

CredentialsLogic.actions.fetchDetails();
try {
await promise;
} catch {
expect(flashAPIErrors).toHaveBeenCalledWith('An error occured');
}
await expectedAsyncError(promise);

expect(flashAPIErrors).toHaveBeenCalledWith('An error occured');
});
});

Expand All @@ -1154,11 +1149,9 @@ describe('CredentialsLogic', () => {
http.delete.mockReturnValue(promise);

CredentialsLogic.actions.deleteApiKey(tokenName);
try {
await promise;
} catch {
expect(flashAPIErrors).toHaveBeenCalledWith('An error occured');
}
await expectedAsyncError(promise);

expect(flashAPIErrors).toHaveBeenCalledWith('An error occured');
});
});

Expand Down Expand Up @@ -1218,11 +1211,9 @@ describe('CredentialsLogic', () => {
http.post.mockReturnValue(promise);

CredentialsLogic.actions.onApiTokenChange();
try {
await promise;
} catch {
expect(flashAPIErrors).toHaveBeenCalledWith('An error occured');
}
await expectedAsyncError(promise);

expect(flashAPIErrors).toHaveBeenCalledWith('An error occured');
});

describe('token type data', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { LogicMounter } from '../../../__mocks__/kea.mock';
import { LogicMounter, mockHttpValues, expectedAsyncError } from '../../../__mocks__';

import { mockHttpValues } from '../../../__mocks__';
jest.mock('../../../shared/http', () => ({
HttpLogic: { values: mockHttpValues },
}));
Expand Down Expand Up @@ -81,12 +80,9 @@ describe('DocumentDetailLogic', () => {
const promise = Promise.reject('An error occurred');
http.get.mockReturnValue(promise);

try {
DocumentDetailLogic.actions.getDocumentDetails('1');
await promise;
} catch {
// Do nothing
}
DocumentDetailLogic.actions.getDocumentDetails('1');
await expectedAsyncError(promise);

expect(flashAPIErrors).toHaveBeenCalledWith('An error occurred', { isQueued: true });
expect(KibanaLogic.values.navigateToUrl).toHaveBeenCalledWith('/engines/engine1/documents');
});
Expand Down Expand Up @@ -134,12 +130,9 @@ describe('DocumentDetailLogic', () => {
promise = Promise.reject('An error occured');
http.delete.mockReturnValue(promise);

try {
DocumentDetailLogic.actions.deleteDocument('1');
await promise;
} catch {
// Do nothing
}
DocumentDetailLogic.actions.deleteDocument('1');
await expectedAsyncError(promise);

expect(flashAPIErrors).toHaveBeenCalledWith('An error occured');
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { LogicMounter } from '../../../__mocks__/kea.mock';
import { LogicMounter, mockHttpValues, expectedAsyncError } from '../../../__mocks__';

import { mockHttpValues } from '../../../__mocks__';
jest.mock('../../../shared/http', () => ({
HttpLogic: { values: mockHttpValues },
}));
Expand Down Expand Up @@ -193,12 +192,9 @@ describe('EngineLogic', () => {
const promise = Promise.reject('An error occured');
http.get.mockReturnValue(promise);

try {
EngineLogic.actions.initializeEngine();
await promise;
} catch {
// Do nothing
}
EngineLogic.actions.initializeEngine();
await expectedAsyncError(promise);

expect(EngineLogic.actions.setEngineNotFound).toHaveBeenCalledWith(true);
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { LogicMounter } from '../../../__mocks__/kea.mock';
import { LogicMounter, mockHttpValues, expectedAsyncError } from '../../../__mocks__';

import { mockHttpValues } from '../../../__mocks__';
jest.mock('../../../shared/http', () => ({
HttpLogic: { values: mockHttpValues },
}));
Expand Down Expand Up @@ -106,12 +105,9 @@ describe('EngineOverviewLogic', () => {
const promise = Promise.reject('An error occurred');
http.get.mockReturnValue(promise);

try {
EngineOverviewLogic.actions.pollForOverviewMetrics();
await promise;
} catch {
// Do nothing
}
EngineOverviewLogic.actions.pollForOverviewMetrics();
await expectedAsyncError(promise);

expect(flashAPIErrors).toHaveBeenCalledWith('An error occurred');
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { LogicMounter } from '../../../__mocks__/kea.mock';
import { LogicMounter, mockHttpValues, expectedAsyncError } from '../../../__mocks__';

import { mockHttpValues } from '../../../__mocks__';
jest.mock('../../../shared/http', () => ({
HttpLogic: { values: mockHttpValues },
}));
Expand Down Expand Up @@ -232,12 +231,8 @@ describe('LogRetentionLogic', () => {
http.put.mockReturnValue(promise);

LogRetentionLogic.actions.saveLogRetention(LogRetentionOptions.Analytics, true);
await expectedAsyncError(promise);

try {
await promise;
} catch {
// Do nothing
}
expect(flashAPIErrors).toHaveBeenCalledWith('An error occured');
expect(LogRetentionLogic.actions.clearLogRetentionUpdating).toHaveBeenCalled();
});
Expand Down Expand Up @@ -305,12 +300,8 @@ describe('LogRetentionLogic', () => {
http.get.mockReturnValue(promise);

LogRetentionLogic.actions.fetchLogRetention();
await expectedAsyncError(promise);

try {
await promise;
} catch {
// Do nothing
}
expect(flashAPIErrors).toHaveBeenCalledWith('An error occured');
expect(LogRetentionLogic.actions.clearLogRetentionUpdating).toHaveBeenCalled();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

import { resetContext } from 'kea';

import { expectedAsyncError } from '../../__mocks__';

jest.mock('../http', () => ({
HttpLogic: {
values: { http: { get: jest.fn() } },
Expand Down Expand Up @@ -82,11 +84,8 @@ describe('IndexingStatusLogic', () => {
IndexingStatusLogic.actions.fetchIndexingStatus({ statusPath, onComplete });
jest.advanceTimersByTime(TIMEOUT);

try {
await promise;
} catch {
// Do nothing
}
await expectedAsyncError(promise);

expect(flashAPIErrors).toHaveBeenCalledWith('An error occured');
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
*/

import { resetContext } from 'kea';
import { mockHttpValues } from '../../../../../__mocks__';

import { mockHttpValues, expectedAsyncError } from '../../../../../__mocks__';

jest.mock('../../../../../shared/http', () => ({
HttpLogic: {
values: { http: mockHttpValues.http },
Expand Down Expand Up @@ -296,11 +298,8 @@ describe('AddSourceLogic', () => {
(HttpLogic.values.http.get as jest.Mock).mockReturnValue(promise);

AddSourceLogic.actions.getSourceConfigData('github');
try {
await promise;
} catch {
// Do nothing
}
await expectedAsyncError(promise);

expect(flashAPIErrors).toHaveBeenCalledWith('this is an error');
});
});
Expand Down Expand Up @@ -345,11 +344,8 @@ describe('AddSourceLogic', () => {
(HttpLogic.values.http.get as jest.Mock).mockReturnValue(promise);

AddSourceLogic.actions.getSourceConnectData('github', successCallback);
try {
await promise;
} catch {
// Do nothing
}
await expectedAsyncError(promise);

expect(flashAPIErrors).toHaveBeenCalledWith('this is an error');
});
});
Expand Down Expand Up @@ -377,11 +373,8 @@ describe('AddSourceLogic', () => {
(HttpLogic.values.http.get as jest.Mock).mockReturnValue(promise);

AddSourceLogic.actions.getSourceReConnectData('github');
try {
await promise;
} catch {
// Do nothing
}
await expectedAsyncError(promise);

expect(flashAPIErrors).toHaveBeenCalledWith('this is an error');
});
});
Expand Down Expand Up @@ -409,11 +402,8 @@ describe('AddSourceLogic', () => {
(HttpLogic.values.http.get as jest.Mock).mockReturnValue(promise);

AddSourceLogic.actions.getPreContentSourceConfigData('123');
try {
await promise;
} catch {
// Do nothing
}
await expectedAsyncError(promise);

expect(flashAPIErrors).toHaveBeenCalledWith('this is an error');
});
});
Expand Down Expand Up @@ -485,11 +475,8 @@ describe('AddSourceLogic', () => {
(HttpLogic.values.http.put as jest.Mock).mockReturnValue(promise);

AddSourceLogic.actions.saveSourceConfig(true);
try {
await promise;
} catch {
// Do nothing
}
await expectedAsyncError(promise);

expect(flashAPIErrors).toHaveBeenCalledWith('this is an error');
});
});
Expand Down Expand Up @@ -550,11 +537,8 @@ describe('AddSourceLogic', () => {
(HttpLogic.values.http.post as jest.Mock).mockReturnValue(promise);

AddSourceLogic.actions.createContentSource(serviceType, successCallback, errorCallback);
try {
await promise;
} catch {
// Do nothing
}
await expectedAsyncError(promise);

expect(errorCallback).toHaveBeenCalled();
expect(flashAPIErrors).toHaveBeenCalledWith('this is an error');
});
Expand Down
Loading

0 comments on commit d2de5d0

Please sign in to comment.