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
npm install @elastic/elasticsearch-mock
Typescript
Module System
Node Version
NPM Version
85.7
Supply Chain
99.5
Quality
83.1
Maintenance
100
Vulnerability
100
License
JavaScript (93.03%)
TypeScript (6.97%)
Verify real, reachable, and deliverable emails with instant MX records, SMTP checks, and disposable email detection.
Total Downloads
5,766,168
Last Day
7,405
Last Week
39,215
Last Month
155,818
Last Year
1,725,232
Apache-2.0 License
49 Stars
72 Commits
13 Forks
4 Watchers
3 Branches
37 Contributors
Updated on Mar 07, 2025
Minified
Minified + Gzipped
Latest Version
2.0.1
Package Id
@elastic/elasticsearch-mock@2.0.1
Unpacked Size
51.09 kB
Size
10.58 kB
File Count
10
NPM Version
10.7.0
Node Version
22.2.0
Published on
Oct 24, 2024
Cumulative downloads
Total Downloads
Last Day
2.7%
7,405
Compared to previous day
Last Week
9.6%
39,215
Compared to previous week
Last Month
5%
155,818
Compared to previous month
Last Year
-6.2%
1,725,232
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
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
security policy file detected
Details
Reason
8 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 6
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/3 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-03-10
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