Gathering detailed insights and metrics for @elastic/elasticsearch-mock
Gathering detailed insights and metrics for @elastic/elasticsearch-mock
Gathering detailed insights and metrics for @elastic/elasticsearch-mock
Gathering detailed insights and metrics for @elastic/elasticsearch-mock
Mock utility for the Elasticsearch's Node.js client
npm install @elastic/elasticsearch-mock
Typescript
Module System
Node Version
NPM Version
97.9
Supply Chain
99.5
Quality
83.6
Maintenance
100
Vulnerability
100
License
JavaScript (93.65%)
TypeScript (6.35%)
Total Downloads
6,426,782
Last Day
1,030
Last Week
42,400
Last Month
167,234
Last Year
1,739,888
Apache-2.0 License
50 Stars
118 Commits
13 Forks
4 Watchers
3 Branches
41 Contributors
Updated on Aug 27, 2025
Latest Version
2.1.0
Package Id
@elastic/elasticsearch-mock@2.1.0
Unpacked Size
54.64 kB
Size
11.69 kB
File Count
11
NPM Version
10.7.0
Node Version
22.2.0
Published on
Apr 18, 2025
Cumulative downloads
Total Downloads
Last Day
-57.6%
1,030
Compared to previous day
Last Week
6.4%
42,400
Compared to previous week
Last Month
5.5%
167,234
Compared to previous month
Last Year
-10.2%
1,739,888
Compared to previous year
3
6
When testing your application you don't always need to have an Elasticsearch instance up and running, but you might still need to use the client for fetching some data. If you are facing this situation, this library is what you need.
Use v1.0.0
for @elastic/elasticsearch
≤ v7 compatibility and v2.0.0
for @elastic/elasticsearch
≥ v8 compatibility.
npm install @elastic/elasticsearch-mock --save-dev
1const { Client } = require('@elastic/elasticsearch') 2const Mock = require('@elastic/elasticsearch-mock') 3 4const mock = new Mock() 5const client = new Client({ 6 node: 'http://localhost:9200', 7 Connection: mock.getConnection() 8}) 9 10mock.add({ 11 method: 'GET', 12 path: '/_cat/health' 13}, () => { 14 return { status: 'ok' } 15}) 16 17client.cat.health() 18 .then(console.log) 19 .catch(console.log)
Constructor
Before start using the library you need to create a new instance:
1const Mock = require('@elastic/elasticsearch-mock') 2const mock = new Mock()
add
Adds a new mock for a given pattern and assigns it to a resolver function.
1// every GET request to the `/_cat/health` path 2// will return `{ status: 'ok' }` 3mock.add({ 4 method: 'GET', 5 path: '/_cat/health' 6}, () => { 7 return { status: 'ok' } 8})
You can also specify multiple methods and/or paths at the same time:
1// This mock will catch every search request against any index 2mock.add({ 3 method: ['GET', 'POST'], 4 path: ['/_search', '/:index/_search'] 5}, () => { 6 return { status: 'ok' } 7})
get
Returns the matching resolver function for the given pattern, it returns null
if there is not a matching pattern.
1const fn = mock.get({ 2 method: 'GET', 3 path: '/_cat/health' 4})
clear
Clears/removes mocks for specific route(s).
1mock.clear({ 2 method: ['GET'], 3 path: ['/_search', '/:index/_search'] 4})
clearAll
Clears all mocks.
1mock.clearAll()
getConnection
Returns a custom Connection
class that you must pass to the Elasticsearch client instance.
1const { Client } = require('@elastic/elasticsearch') 2const Mock = require('@elastic/elasticsearch-mock') 3 4const mock = new Mock() 5const client = new Client({ 6 node: 'http://localhost:9200', 7 Connection: mock.getConnection() 8})
A pattern is an object that describes an http query to Elasticsearch, and it looks like this:
1interface MockPattern { 2 method: string 3 path: string 4 querystring?: Record<string, string> 5 body?: Record<string, any> 6}
The more field you specify, the more the mock will be strict, for example:
1mock.add({ 2 method: 'GET', 3 path: '/_cat/health' 4 querystring: { pretty: 'true' } 5}, () => { 6 return { status: 'ok' } 7}) 8 9client.cat.health() 10 .then(console.log) 11 .catch(console.log) // 404 error 12 13client.cat.health({ pretty: true }) 14 .then(console.log) // { status: 'ok' } 15 .catch(console.log)
You can craft custom responses for different queries:
1mock.add({ 2 method: 'POST', 3 path: '/indexName/_search' 4 body: { query: { match_all: {} } } 5}, () => { 6 return { 7 hits: { 8 total: { value: 1, relation: 'eq' }, 9 hits: [{ _source: { baz: 'faz' } }] 10 } 11 } 12}) 13 14mock.add({ 15 method: 'POST', 16 path: '/indexName/_search', 17 body: { query: { match: { foo: 'bar' } } } 18}, () => { 19 return { 20 hits: { 21 total: { value: 0, relation: 'eq' }, 22 hits: [] 23 } 24 } 25})
You can also specify dynamic urls:
1mock.add({ 2 method: 'GET', 3 path: '/:index/_count' 4}, () => { 5 return { count: 42 } 6}) 7 8client.count({ index: 'foo' }) 9 .then(console.log) // => { count: 42 } 10 .catch(console.log) 11 12client.count({ index: 'bar' }) 13 .then(console.log) // => { count: 42 } 14 .catch(console.log)
Wildcards are supported as well.
1mock.add({ 2 method: 'HEAD', 3 path: '*' 4}, () => { 5 return '' 6}) 7 8client.indices.exists({ index: 'foo' }) 9 .then(console.log) // => true 10 .catch(console.log) 11 12client.ping() 13 .then(console.log) // => true 14 .catch(console.log)
The resolver function takes a single parameter which represent the API call that has been made by the client. You can use it to craft dynamic responses.
1mock.add({ 2 method: 'POST', 3 path: '/indexName/_search', 4}, params => { 5 return { query: params.body.query } 6})
This utility uses the same error classes of the Elasticsearch client, if you want to return an error for a specific API call, you should use the ResponseError
class:
1const { errors } = require('@elastic/elasticsearch') 2const Mock = require('@elastic/elasticsearch-mock') 3 4const mock = new Mock() 5mock.add({ 6 method: 'GET', 7 path: '/_cat/health' 8}, () => { 9 return new errors.ResponseError({ 10 body: { errors: {}, status: 500 }, 11 statusCode: 500 12 }) 13})
This software is licensed under the Apache 2 license.
No vulnerabilities found.
elasticsearch-mock-js
Creates an elasticsearch server for mocking purposes
@vestaboard/elasticsearch-mocks
ElasticSearch Mocking Library for Jest
@short.io/opensearch-mock
Mock utility for the Elasticsearch's Node.js client
mock-es-for-node
A lightweight in-memory ElasticSearch mock for Node.js unit testing with TypeScript support