Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@elastic/elasticsearch-mock

Package Overview
Dependencies
Maintainers
64
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@elastic/elasticsearch-mock

Mock utility for the Elasticsearch's Node.js client

  • 0.1.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
35K
increased by15.55%
Maintainers
64
Weekly downloads
 
Created
Source

Elasticsearch Node.js client mock utility

js-standard-style build

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 @elastic/elasticsearch-mock --save-dev

Usage

const { Client } = require('@elastic/elasticsearch')
const Mock = require('@elastic/elasticsearch-mock')

const mock = new Mock()
const client = new Client({
  node: 'http://localhost:9200',
  Connection: mock.getConnection()
})

mock.add({
  method: 'GET',
  path: '/'
}, () => {
  return { status: 'ok' }
})

client.info(console.log)

API

Constructor

Before start using the library you need to create a new instance:

const Mock = require('@elastic/elasticsearch-mock')
const mock = new Mock()
add

Adds a new mock for a given pattern and assigns it to a resolver function.

// every GET request to the `/` path
// will return `{ status: 'ok' }`
mock.add({
  method: 'GET',
  path: '/'
}, () => {
  return { status: 'ok' }
})
get

Returns the matching resolver function for the given pattern, it returns null if there is not a matching pattern.

const fn = mock.get({
  method: 'GET',
  path: '/'
})
getConnection

Returns a custom Connection class that you must pass to the Elasticsearch client instance.

const { Client } = require('@elastic/elasticsearch')
const Mock = require('@elastic/elasticsearch-mock')

const mock = new Mock()
const client = new Client({
  node: 'http://localhost:9200',
  Connection: mock.getConnection()
})

Mock patterns

A pattern is an object that describes an http query to Elasticsearch, and it looks like this:

interface MockPattern {
  method: string
  path: string
  querystring?: Record<string, string>
  body?: Record<string, any>
}

The more field you specify, the more the mock will be strict, for example:

mock.add({
  method: 'GET',
  path: '/',
  querystring: { pretty: 'true' }
}, () => {
  return { status: 'ok' }
})

client.info(console.log) // => 404 error
client.info({ pretty: true }, console.log) // => { status: 'ok' }

You can craft custom responses for different queries:

mock.add({
  method: 'POST',
  path: '/indexName/_search'
  body: { query: { match_all: {} } }
}, () => {
  return {
    hits: {
      total: { value: 1, relation: 'eq' },
      hits: [{ _source: { baz: 'faz' } }]
    }
  }
})

mock.add({
  method: 'POST',
  path: '/indexName/_search',
  body: { query: { match: { foo: 'bar' } } }
}, () => {
  return {
    hits: {
      total: { value: 0, relation: 'eq' },
      hits: []
    }
  }
})

You can also specify dynamic urls:

mock.add({
  method: 'GET',
  path: '/:index/_count'
}, () => {
  return { count: 42 }
})

client.count({ index: 'foo' }, console.log) // => { count: 42 }
client.count({ index: 'bar' }, console.log) // => { count: 42 }

Wildcards are supported as well.

mock.add({
  method: 'HEAD',
  path: '*'
}, () => {
  return { status: 'ok' }
})

client.indices.exists({ index: 'foo' }, console.log) // => { status: 'ok' }
client.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.

mock.add({
  method: 'POST',
  path: '/indexName/_search',
}, params => {
  return { query: params.body.query }
})

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:

const { errors } = require('@elastic/elasticsearch')
const Mock = require('@elastic/elasticsearch-mock')

const mock = new Mock()
mock.add({
  method: 'GET',
  path: '/'
}, () => {
  return new errors.ResponseError({
    body: { errors: {}, status: 500 },
    statusCode: 500
  })
})

License

This software is licensed under the Apache 2 license.

Keywords

FAQs

Package last updated on 10 Apr 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc