Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What should be added to use @shelf/jest-elasticsearch? #2

Open
wants to merge 3 commits into
base: no-jest-setup
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions jest-es-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const {index} = require('./src/elastic.js');
module.exports = () => {
return {
esVersion: '8.4.0',
clusterName: 'things-cluster',
nodeName: 'things-node',
port: 9200,
indexes: [
{
name: index,
body: {
settings: {
number_of_shards: '1',
number_of_replicas: '1'
},
mappings: {
dynamic: false,
properties: {
id: {
type: 'keyword'
},
value: {
type: 'integer'
},
type: {
type: 'keyword'
},
name: {
type: 'keyword'
},
}
}
}
}
]
};
};
7 changes: 7 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
preset: '@shelf/jest-elasticsearch',
clearMocks: true,
collectCoverage: true,
coverageDirectory: "coverage",
coverageProvider: "v8"
};
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"description": "Example of mocking elasticsearch with jest plugin",
"main": "index.js",
"scripts": {
"test": "jest",
"serve": "node src/index.js"
},
"repository": {
Expand All @@ -25,12 +26,17 @@
"dependencies": {
"@elastic/elasticsearch": "8.5.0",
"@hapi/hapi": "21.0.0",
"@types/jest": "^29.2.3",
"dotenv": "^16.0.3",
"express": "4.18.2",
"qs": "6.11.0",
"ulid": "2.3.0"
},
"engines": {
"node": ">=18"
},
"devDependencies": {
"@shelf/jest-elasticsearch": "5.0.0",
"jest": "^29.3.1"
}
}
4 changes: 4 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Example of how to mock Elastic to run Jest tests

In this repo you can see an example of mocking ElasticSeach in simple CRUD Hapi api with Jest.
Core plugin that was used - [jest-elasticsearch](https://github.com/shelfio/jest-elasticsearch)
57 changes: 57 additions & 0 deletions src/create/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const {create} = require("./index.js");
const {client, index} = require("../elastic");

describe('#create', () => {
beforeEach(async () => {
await client.deleteByQuery({
index,
query: {
match_all: {}
}
})
await client.indices.refresh({index})
})
it('should insert data', async () => {
expect.assertions(3);
const res = await create({type: 'some', value: 100, name: 'jacket'})
await client.indices.refresh();
const data = await client.search({
index,
query: {
match: {
"id": res
}
}
})

expect(res).toEqual(expect.any(String))
expect(res).toHaveLength(26);
expect(data.hits.hits[0]._source).toEqual({
"id": res,
"name": "jacket",
"type": "some",
"value": 100
}
);
})

it('should insert and process the inserted fields', async () => {
const res = await create({type: 'UPPERCASE', value: 25.99, name: ' spaces '})
await client.indices.refresh();
const data = await client.search({
index,
query: {
match: {
"id": res
}
}
})
expect(data.hits.hits[0]._source).toEqual({
"id": res,
"name": "spaces",
"type": "uppercase",
"value": 26
}
);
})
});
77 changes: 77 additions & 0 deletions src/delete/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
const {deleteAll, deleteById} = require("./index.js");
const {client, index} = require("../elastic");
const {ulid} = require("ulid");

describe('#deleteAll', () => {
beforeEach(async () => {
await client.deleteByQuery({
index,
query: {
match_all: {}
}
})
await client.indices.refresh({index})
})
it('should delete all records', async () => {
await client.index({
index,
document: {type: 'some', value: 222, name: 'jacket'}
})
await client.indices.refresh({index})

await deleteAll();

await client.indices.refresh({index})
const data = await client.search({
index,
query: {
match_all: {}
}
})

expect(data.hits).toEqual({
"hits": [],
"max_score": null,
"total": {
"relation": "eq",
"value": 0
}
})

})
});

describe('#deleteById', () => {
it('should delete specific record', async () => {
const id = 'some-id'
await Promise.all([
client.index({
index,
document: {type: 'some', value: 222, name: 'jacket', id}
}),
client.index({
index,
document: {type: 'some2', value: 2332, name: 'jacket2', id: ulid()}
})
])
await client.indices.refresh({index})

await deleteById(id);

await client.indices.refresh({index})
const data = await client.search({
index,
query: {
match_all: {}
}
})

expect(data.hits.hits[0]._source).toEqual({
"id": expect.any(String),
"name": "jacket2",
"type": "some2",
"value": 2332
})

})
});
85 changes: 85 additions & 0 deletions src/read/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
const {readAll, read} = require('./index');
const {client, index} = require("../elastic");
const {ulid} = require("ulid");

describe('#readAll', () => {
beforeEach(async () => {
await client.deleteByQuery({
index,
query: {
match_all: {}
}
})
await client.indices.refresh({index})
})
it('should return objects with all field', async () => {
await Promise.all([
client.index({
index,
document: {type: 'some', value: 222, name: 'jacket', id: ulid()}
}),
client.index({
index,
document: {type: 'some2', value: 2332, name: 'jacket2', id: ulid()}
})
])
await client.indices.refresh({index})

const source = await readAll()

expect(source).toEqual([
{
"id": expect.any(String),
"name": "jacket",
"type": "some",
"value": 222
},
{
"id": expect.any(String),
"name": "jacket2",
"type": "some2",
"value": 2332
},
]);
})
it('should return empty array even if not values are present', async () => {
const source = await readAll();
expect(source).toEqual([])
});
})

describe('#read', () => {
beforeEach(async () => {
await client.deleteByQuery({
index,
query: {
match_all: {}
}
})
await client.indices.refresh({index})
})
it('should return object with all field', async () => {
await Promise.all([
client.index({
index,
document: {type: 'some', value: 222, name: 'jacket', id: 'some-id'}
}),
client.index({
index,
document: {type: 'some2', value: 2332, name: 'jacket2', id: ulid()}
})
])
await client.indices.refresh({index})

const source = await read('some-id')

expect(source).toEqual(
{
"id": "some-id",
"name": "jacket",
"type": "some",
"value": 222
}
);
})
})
89 changes: 89 additions & 0 deletions src/update/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
const {client, index} = require("../elastic");
const {updateById} = require("./index");

describe('#updateById', () => {
beforeEach(async () => {
await client.deleteByQuery({
index,
query: {
match_all: {}
}
})
await client.indices.refresh({index})
})
it('should return objects with updated `value` field', async () => {

await client.index({
index,
document: {type: 'some', value: 222, name: 'jacket', id: 'some-id'}
})
await client.indices.refresh({index})

await updateById('some-id', {value: 3.44865});
await client.indices.refresh({index})
const updated = await client.search({
index,
query: {
match: {
id: 'some-id'
}
}
})
expect(updated.hits.hits[0]._source).toEqual({
"id": "some-id",
"name": "jacket",
"type": "some",
"value": 3
});
})

it('should return objects with proper updated `type` field', async () => {
await client.index({
index,
document: {type: 'some', value: 222, name: 'jacket', id: 'some-id'}
})
await client.indices.refresh({index})

await updateById('some-id', {type: ' SomE333 '});
await client.indices.refresh({index})
const updated = await client.search({
index,
query: {
match: {
id: 'some-id'
}
}
})
expect(updated.hits.hits[0]._source).toEqual({
"id": "some-id",
"name": "jacket",
"type": "some333",
"value": 222
});
})

it('should return objects with proper updated `name` field', async () => {
await client.index({
index,
document: {type: 'some', value: 222, name: 'jacket', id: 'some-id'}
})
await client.indices.refresh({index})

await updateById('some-id', {name: ' SomE333 '});
await client.indices.refresh({index})
const updated = await client.search({
index,
query: {
match: {
id: 'some-id'
}
}
})
expect(updated.hits.hits[0]._source).toEqual({
"id": "some-id",
"name": "SomE333",
"type": "some",
"value": 222
});
})
})
Loading