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

@elastic/elasticsearch-mock

Package Overview
Dependencies
Maintainers
68
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@elastic/elasticsearch-mock - npm Package Compare versions

Comparing version 0.2.1 to 0.3.0

2

index.d.ts

@@ -11,2 +11,4 @@ // Licensed to Elasticsearch B.V under one or more agreements.

get(pattern: MockPattern): ResolverFn | null
clear(pattern: Pick<MockPattern, 'method' | 'path'>): ClientMock
clearAll(): ClientMock
getConnection(): typeof Connection

@@ -13,0 +15,0 @@ }

@@ -79,2 +79,24 @@ // Licensed to Elasticsearch B.V under one or more agreements.

clear (pattern) {
for (const key of ['method', 'path']) {
if (Array.isArray(pattern[key])) {
for (const value of pattern[key]) {
this.clear({ ...pattern, [key]: value })
}
return this
}
}
if (typeof pattern.method !== 'string') throw new ConfigurationError('The method is not defined')
if (typeof pattern.path !== 'string') throw new ConfigurationError('The path is not defined')
this[kRouter].off(pattern.method, pattern.path)
return this
}
clearAll () {
this[kRouter].reset()
return this
}
getConnection () {

@@ -81,0 +103,0 @@ return buildConnectionClass(this)

2

package.json
{
"name": "@elastic/elasticsearch-mock",
"version": "0.2.1",
"version": "0.3.0",
"description": "Mock utility for the Elasticsearch's Node.js client",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -89,2 +89,21 @@ <img align="right" width="auto" height="auto" src="https://www.elastic.co/static-res/images/elastic-logo-200.png">

#### `clear`
Clears/removes mocks for specific route(s).
```js
mock.clear({
method: ['GET'],
path: ['/_search', '/:index/_search']
})
```
#### `clearAll`
Clears all mocks.
```js
mock.clearAll()
```
#### `getConnection`

@@ -91,0 +110,0 @@

@@ -781,1 +781,101 @@ // Licensed to Elasticsearch B.V under one or more agreements.

})
test('Should clear individual mocks', async t => {
const mock = new Mock()
const client = new Client({
node: 'http://localhost:9200',
Connection: mock.getConnection()
})
mock.add({
method: 'GET',
path: ['/test1/_search', '/test2/_search']
}, () => {
return { status: 'ok' }
})
// Clear test1 but not test2
mock.clear({ method: 'GET', path: ['/test1/_search'] })
// test2 still works
const response = await client.search({
index: 'test2',
q: 'foo:bar'
})
t.deepEqual(response.body, { status: 'ok' })
t.is(response.statusCode, 200)
// test1 does not
try {
await client.search({
index: 'test1',
q: 'foo:bar'
})
t.fail('Should throw')
} catch (err) {
t.true(err instanceof errors.ResponseError)
t.deepEqual(err.body, { error: 'Mock not found' })
t.is(err.statusCode, 404)
}
})
test('.mock should throw if method and path are not defined', async t => {
const mock = new Mock()
try {
mock.clear({ path: '/' }, () => {})
t.fail('Should throw')
} catch (err) {
t.true(err instanceof errors.ConfigurationError)
t.is(err.message, 'The method is not defined')
}
try {
mock.clear({ method: 'GET' }, () => {})
t.fail('Should throw')
} catch (err) {
t.true(err instanceof errors.ConfigurationError)
t.is(err.message, 'The path is not defined')
}
})
test('Should clear all mocks', async t => {
const mock = new Mock()
const client = new Client({
node: 'http://localhost:9200',
Connection: mock.getConnection()
})
mock.add({
method: 'GET',
path: ['/test1/_search', '/test2/_search']
}, () => {
return { status: 'ok' }
})
// Clear mocks
mock.clearAll()
try {
await client.search({
index: 'test1',
q: 'foo:bar'
})
t.fail('Should throw')
} catch (err) {
t.true(err instanceof errors.ResponseError)
t.deepEqual(err.body, { error: 'Mock not found' })
t.is(err.statusCode, 404)
}
try {
await client.search({
index: 'test2',
q: 'foo:bar'
})
t.fail('Should throw')
} catch (err) {
t.true(err instanceof errors.ResponseError)
t.deepEqual(err.body, { error: 'Mock not found' })
t.is(err.statusCode, 404)
}
})
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