mock the whole ES client in the integration tests (#65081)

* mock the whole ES client to prevent real requests

* mock the whole client

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
This commit is contained in:
Mikhail Shustov 2020-05-04 19:57:51 +02:00 committed by GitHub
parent d94e6d38b7
commit f98f4f27e0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 66 additions and 4 deletions

View file

@ -18,6 +18,7 @@
*/
import { BehaviorSubject } from 'rxjs';
import { Client } from 'elasticsearch';
import { IClusterClient, ICustomClusterClient } from './cluster_client';
import { IScopedClusterClient } from './scoped_cluster_client';
import { ElasticsearchConfig } from './elasticsearch_config';
@ -130,6 +131,55 @@ const createMock = () => {
return mocked;
};
const createElasticsearchClientMock = () => {
const mocked: jest.Mocked<Client> = {
cat: {} as any,
cluster: {} as any,
indices: {} as any,
ingest: {} as any,
nodes: {} as any,
snapshot: {} as any,
tasks: {} as any,
bulk: jest.fn(),
clearScroll: jest.fn(),
count: jest.fn(),
create: jest.fn(),
delete: jest.fn(),
deleteByQuery: jest.fn(),
deleteScript: jest.fn(),
deleteTemplate: jest.fn(),
exists: jest.fn(),
explain: jest.fn(),
fieldStats: jest.fn(),
get: jest.fn(),
getScript: jest.fn(),
getSource: jest.fn(),
getTemplate: jest.fn(),
index: jest.fn(),
info: jest.fn(),
mget: jest.fn(),
msearch: jest.fn(),
msearchTemplate: jest.fn(),
mtermvectors: jest.fn(),
ping: jest.fn(),
putScript: jest.fn(),
putTemplate: jest.fn(),
reindex: jest.fn(),
reindexRethrottle: jest.fn(),
renderSearchTemplate: jest.fn(),
scroll: jest.fn(),
search: jest.fn(),
searchShards: jest.fn(),
searchTemplate: jest.fn(),
suggest: jest.fn(),
termvectors: jest.fn(),
update: jest.fn(),
updateByQuery: jest.fn(),
close: jest.fn(),
};
return mocked;
};
export const elasticsearchServiceMock = {
create: createMock,
createInternalSetup: createInternalSetupContractMock,
@ -138,4 +188,5 @@ export const elasticsearchServiceMock = {
createClusterClient: createClusterClientMock,
createCustomClusterClient: createCustomClusterClientMock,
createScopedClusterClient: createScopedClusterClientMock,
createElasticsearchClient: createElasticsearchClientMock,
};

View file

@ -24,3 +24,14 @@ jest.doMock('../../elasticsearch/scoped_cluster_client', () => ({
return elasticsearchServiceMock.createScopedClusterClient();
}),
}));
jest.doMock('elasticsearch', () => {
const realES = jest.requireActual('elasticsearch');
return {
...realES,
// eslint-disable-next-line object-shorthand
Client: function() {
return elasticsearchServiceMock.createElasticsearchClient();
},
};
});

View file

@ -43,7 +43,7 @@ describe('http service', () => {
describe('auth', () => {
let root: ReturnType<typeof kbnTestServer.createRoot>;
beforeEach(async () => {
root = kbnTestServer.createRoot({ migrations: { skip: true } });
root = kbnTestServer.createRoot();
}, 30000);
afterEach(async () => {
@ -192,7 +192,7 @@ describe('http service', () => {
let root: ReturnType<typeof kbnTestServer.createRoot>;
beforeEach(async () => {
root = kbnTestServer.createRoot({ migrations: { skip: true } });
root = kbnTestServer.createRoot();
}, 30000);
afterEach(async () => {
@ -326,7 +326,7 @@ describe('http service', () => {
describe('#basePath()', () => {
let root: ReturnType<typeof kbnTestServer.createRoot>;
beforeEach(async () => {
root = kbnTestServer.createRoot({ migrations: { skip: true } });
root = kbnTestServer.createRoot();
}, 30000);
afterEach(async () => await root.shutdown());
@ -355,7 +355,7 @@ describe('http service', () => {
describe('elasticsearch', () => {
let root: ReturnType<typeof kbnTestServer.createRoot>;
beforeEach(async () => {
root = kbnTestServer.createRoot({ migrations: { skip: true } });
root = kbnTestServer.createRoot();
}, 30000);
afterEach(async () => {