{"id":13837579,"url":"https://github.com/elastic/elasticsearch-js-mock","last_synced_at":"2025-04-04T12:10:12.193Z","repository":{"id":43259118,"uuid":"254338010","full_name":"elastic/elasticsearch-js-mock","owner":"elastic","description":"Mock utility for the Elasticsearch's Node.js client","archived":false,"fork":false,"pushed_at":"2025-03-24T17:19:39.000Z","size":72,"stargazers_count":49,"open_issues_count":20,"forks_count":13,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-03-30T22:13:48.433Z","etag":null,"topics":["client","elasticsearch","mock","nodejs","rest","testing-tools"],"latest_commit_sha":null,"homepage":"https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/client-testing.html","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/elastic.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-04-09T10:15:30.000Z","updated_at":"2025-03-24T17:17:33.000Z","dependencies_parsed_at":"2024-08-30T17:05:50.975Z","dependency_job_id":"51672923-c401-46eb-9541-2e8965f2e7c8","html_url":"https://github.com/elastic/elasticsearch-js-mock","commit_stats":{"total_commits":35,"total_committers":3,"mean_commits":"11.666666666666666","dds":"0.17142857142857137","last_synced_commit":"d8b53c656691073386d816165cdaa63336da36f4"},"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/elastic%2Felasticsearch-js-mock","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/elastic%2Felasticsearch-js-mock/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/elastic%2Felasticsearch-js-mock/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/elastic%2Felasticsearch-js-mock/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/elastic","download_url":"https://codeload.github.com/elastic/elasticsearch-js-mock/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247174454,"owners_count":20896078,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["client","elasticsearch","mock","nodejs","rest","testing-tools"],"created_at":"2024-08-04T15:01:15.245Z","updated_at":"2025-04-04T12:10:12.177Z","avatar_url":"https://github.com/elastic.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"\u003cimg align=\"right\" width=\"auto\" height=\"auto\" src=\"https://www.elastic.co/static-res/images/elastic-logo-200.png\"\u003e\n\n# Elasticsearch Node.js client mock utility\n\n[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](http://standardjs.com/)  ![build](https://github.com/elastic/elasticsearch-js-mock/workflows/build/badge.svg)\n\nWhen testing your application you don't always need to have an Elasticsearch instance up and running,\nbut you might still need to use the client for fetching some data.\nIf you are facing this situation, this library is what you need.\n\nUse `v1.0.0` for `@elastic/elasticsearch` ≤ v7 compatibility and `v2.0.0` for  `@elastic/elasticsearch` ≥ v8 compatibility.\n\n### Features\n\n- Simple and intuitive API\n- Mocks only the http layer, leaving the rest of the client working as usual\n- Maximum flexibility thanks to \"strict\" or \"loose\" mocks\n\n## Install\n```\nnpm install @elastic/elasticsearch-mock --save-dev\n```\n\n## Usage\n\n```js\nconst { Client } = require('@elastic/elasticsearch')\nconst Mock = require('@elastic/elasticsearch-mock')\n\nconst mock = new Mock()\nconst client = new Client({\n  node: 'http://localhost:9200',\n  Connection: mock.getConnection()\n})\n\nmock.add({\n  method: 'GET',\n  path: '/_cat/health'\n}, () =\u003e {\n  return { status: 'ok' }\n})\n\nclient.cat.health()\n  .then(console.log)\n  .catch(console.log)\n```\n\n## API\n\n#### `Constructor`\n\nBefore start using the library you need to create a new instance:\n```js\nconst Mock = require('@elastic/elasticsearch-mock')\nconst mock = new Mock()\n```\n\n#### `add`\n\nAdds a new mock for a given pattern and assigns it to a resolver function.\n\n```js\n// every GET request to the `/_cat/health` path\n// will return `{ status: 'ok' }`\nmock.add({\n  method: 'GET',\n  path: '/_cat/health'\n}, () =\u003e {\n  return { status: 'ok' }\n})\n```\n\nYou can also specify multiple methods and/or paths at the same time:\n```js\n// This mock will catch every search request against any index\nmock.add({\n  method: ['GET', 'POST'],\n  path: ['/_search', '/:index/_search']\n}, () =\u003e {\n  return { status: 'ok' }\n})\n```\n\n#### `get`\n\nReturns the matching resolver function for the given pattern, it returns `null` if there is not a matching pattern.\n\n```js\nconst fn = mock.get({\n  method: 'GET',\n  path: '/_cat/health'\n})\n```\n\n#### `clear`\n\nClears/removes mocks for specific route(s).\n\n```js\nmock.clear({\n  method: ['GET'],\n  path: ['/_search', '/:index/_search']\n})\n```\n\n#### `clearAll`\n\nClears all mocks.\n\n```js\nmock.clearAll()\n```\n\n#### `getConnection`\n\nReturns a custom `Connection` class that you **must** pass to the Elasticsearch client instance.\n\n```js\nconst { Client } = require('@elastic/elasticsearch')\nconst Mock = require('@elastic/elasticsearch-mock')\n\nconst mock = new Mock()\nconst client = new Client({\n  node: 'http://localhost:9200',\n  Connection: mock.getConnection()\n})\n```\n\n### Mock patterns\n\nA pattern is an object that describes an http query to Elasticsearch, and it looks like this:\n```ts\ninterface MockPattern {\n  method: string\n  path: string\n  querystring?: Record\u003cstring, string\u003e\n  body?: Record\u003cstring, any\u003e\n}\n```\n\nThe more field you specify, the more the mock will be strict, for example:\n```js\nmock.add({\n  method: 'GET',\n  path: '/_cat/health'\n  querystring: { pretty: 'true' }\n}, () =\u003e {\n  return { status: 'ok' }\n})\n\nclient.cat.health()\n  .then(console.log)\n  .catch(console.log) // 404 error\n\nclient.cat.health({ pretty: true })\n  .then(console.log) // { status: 'ok' }\n  .catch(console.log)\n```\n\nYou can craft custom responses for different queries:\n\n```js\nmock.add({\n  method: 'POST',\n  path: '/indexName/_search'\n  body: { query: { match_all: {} } }\n}, () =\u003e {\n  return {\n    hits: {\n      total: { value: 1, relation: 'eq' },\n      hits: [{ _source: { baz: 'faz' } }]\n    }\n  }\n})\n\nmock.add({\n  method: 'POST',\n  path: '/indexName/_search',\n  body: { query: { match: { foo: 'bar' } } }\n}, () =\u003e {\n  return {\n    hits: {\n      total: { value: 0, relation: 'eq' },\n      hits: []\n    }\n  }\n})\n```\n\nYou can also specify dynamic urls:\n```js\nmock.add({\n  method: 'GET',\n  path: '/:index/_count'\n}, () =\u003e {\n  return { count: 42 }\n})\n\nclient.count({ index: 'foo' })\n  .then(console.log) // =\u003e { count: 42 }\n  .catch(console.log)\n\nclient.count({ index: 'bar' })\n  .then(console.log) // =\u003e { count: 42 }\n  .catch(console.log)\n```\n\nWildcards are supported as well.\n```js\nmock.add({\n  method: 'HEAD',\n  path: '*'\n}, () =\u003e {\n  return ''\n})\n\nclient.indices.exists({ index: 'foo' })\n  .then(console.log) // =\u003e true\n  .catch(console.log)\n\nclient.ping()\n  .then(console.log) // =\u003e true\n  .catch(console.log)\n```\n\n### Dynamic responses\n\nThe resolver function takes a single parameter which represent the API call that has been made by the client.\nYou can use it to craft dynamic responses.\n\n```js\nmock.add({\n  method: 'POST',\n  path: '/indexName/_search',\n}, params =\u003e {\n  return { query: params.body.query }\n})\n```\n\n### Errors\n\nThis 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:\n\n```js\nconst { errors } = require('@elastic/elasticsearch')\nconst Mock = require('@elastic/elasticsearch-mock')\n\nconst mock = new Mock()\nmock.add({\n  method: 'GET',\n  path: '/_cat/health'\n}, () =\u003e {\n  return new errors.ResponseError({\n    body: { errors: {}, status: 500 },\n    statusCode: 500\n  })\n})\n```\n\n## License\n\nThis software is licensed under the [Apache 2 license](./LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Felastic%2Felasticsearch-js-mock","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Felastic%2Felasticsearch-js-mock","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Felastic%2Felasticsearch-js-mock/lists"}