Installations
npm install @short.io/opensearch-mock
Developer Guide
Typescript
Yes
Module System
CommonJS
Node Version
20.16.0
NPM Version
10.8.1
Score
80.3
Supply Chain
99.1
Quality
77.6
Maintenance
100
Vulnerability
100
License
Releases
Contributors
Unable to fetch Contributors
Languages
JavaScript (93.03%)
TypeScript (6.97%)
validate.email 🚀
Verify real, reachable, and deliverable emails with instant MX records, SMTP checks, and disposable email detection.
Developer
Download Statistics
Total Downloads
398,091
Last Day
181
Last Week
4,042
Last Month
16,990
Last Year
164,537
GitHub Statistics
Apache-2.0 License
49 Stars
72 Commits
13 Forks
4 Watchers
3 Branches
37 Contributors
Updated on Mar 07, 2025
Bundle Size
73.19 kB
Minified
21.75 kB
Minified + Gzipped
Package Meta Information
Latest Version
0.4.0
Package Id
@short.io/opensearch-mock@0.4.0
Unpacked Size
47.41 kB
Size
10.32 kB
File Count
10
NPM Version
10.8.1
Node Version
20.16.0
Published on
Sep 20, 2024
Total Downloads
Cumulative downloads
Total Downloads
398,091
Last Day
40.3%
181
Compared to previous day
Last Week
-15.5%
4,042
Compared to previous week
Last Month
-5.4%
16,990
Compared to previous month
Last Year
20.2%
164,537
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
3
Dev Dependencies
5
Opensearch Node.js client mock utility
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.
Features
- Simple and intuitive API
- Mocks only the http layer, leaving the rest of the client working as usual
- Maximum flexibility thanks to "strict" or "loose" mocks
Install
npm install @short.io/opensearch-js-mock --save-dev
Usage
1const { Client } = require('@opensearch-project/opensearch') 2const Mock = require('@opensearch/opensearch-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(console.log)
API
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})
Mock patterns
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(console.log) // => 404 error 10client.cat.health({ pretty: true }, console.log) // => { status: 'ok' }
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' }, console.log) // => { count: 42 } 9client.count({ index: 'bar' }, console.log) // => { count: 42 }
Wildcards are supported as well.
1mock.add({ 2 method: 'HEAD', 3 path: '*' 4}, () => { 5 return { status: 'ok' } 6}) 7 8client.indices.exists({ index: 'foo' }, console.log) // => { status: 'ok' } 9client.ping(console.log) // => { status: 'ok' }
Dynamic responses
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})
Errors
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})
License
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
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: Apache License 2.0: LICENSE:0
Reason
security policy file detected
Details
- Info: security policy file detected: github.com/elastic/.github/SECURITY.md:1
- Info: Found linked content: github.com/elastic/.github/SECURITY.md:1
- Info: Found disclosure, vulnerability, and/or timelines in security policy: github.com/elastic/.github/SECURITY.md:1
- Info: Found text in security policy: github.com/elastic/.github/SECURITY.md:1
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
- Warn: npmCommand not pinned by hash: .github/workflows/build.yml:27
- Warn: npmCommand not pinned by hash: .github/workflows/build.yml:46
- Info: 4 out of 4 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 2 npmCommand dependencies pinned
Reason
branch protection is not maximal on development and all release branches
Details
- Info: 'allow deletion' disabled on branch 'main'
- Info: 'force pushes' disabled on branch 'main'
- Warn: 'branch protection settings apply to administrators' is disabled on branch 'main'
- Warn: 'stale review dismissal' is disabled on branch 'main'
- Warn: required approving review count is 1 on branch 'main'
- Warn: codeowners review is not required on branch 'main'
- Warn: 'last push approval' is disabled on branch 'main'
- Warn: no status checks found to merge onto branch 'main'
- Info: PRs are required in order to make changes on branch 'main'
Reason
Found 0/3 approved changesets -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/build.yml:1
- Info: no jobLevel write permissions found
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 30 are checked with a SAST tool
Score
5.5
/10
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