Skip to content

Commit

Permalink
mock the whole ES client in the integration tests (elastic#65081)
Browse files Browse the repository at this point in the history
* mock the whole ES client to prevent real requests

* mock the whole client

Co-authored-by: Elastic Machine <[email protected]>
  • Loading branch information
mshustov and elasticmachine committed May 4, 2020
1 parent 4be8446 commit 5987765
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 4 deletions.
51 changes: 51 additions & 0 deletions src/core/server/elasticsearch/elasticsearch_service.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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,
Expand All @@ -138,4 +188,5 @@ export const elasticsearchServiceMock = {
createClusterClient: createClusterClientMock,
createCustomClusterClient: createCustomClusterClientMock,
createScopedClusterClient: createScopedClusterClientMock,
createElasticsearchClient: createElasticsearchClientMock,
};
11 changes: 11 additions & 0 deletions src/core/server/http/integration_tests/core_service.test.mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
},
};
});
8 changes: 4 additions & 4 deletions src/core/server/http/integration_tests/core_services.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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 () => {
Expand Down

0 comments on commit 5987765

Please sign in to comment.