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
JavaScript (93.65%)
TypeScript (6.35%)
Total Downloads
6,044,458
Last Day
5,963
Last Week
33,954
Last Month
144,167
Last Year
1,708,842
Apache-2.0 License
49 Stars
98 Commits
13 Forks
4 Watchers
3 Branches
39 Contributors
Updated on May 05, 2025
Minified
Minified + Gzipped
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
53.3%
5,963
Compared to previous day
Last Week
4.4%
33,954
Compared to previous week
Last Month
-8.2%
144,167
Compared to previous month
Last Year
-10.2%
1,708,842
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.
Reason
30 commit(s) and 4 issue activity found in the last 90 days -- score normalized to 10
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
security policy file detected
Details
Reason
dependency not pinned by hash detected -- score normalized to 6
Details
Reason
branch protection is not maximal on development and all release branches
Details
Reason
Found 0/5 approved changesets -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2025-05-05
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More