It took longer than I expect, but I manage to get it to work with the help from Roxanne. There was a lot meetings and keeping each other up to date on our progression. I have learned a lot in the past few weeks.
setup
If you haven't read my last post. It was about how to create the initial setup. Link. TLDR. We needed to create the jest setup
and the config
file. After that we create the test
file. This will contain all of the different tests.
Mock Elastic search connection
I was stuck on this part for the longest time.
Problems
First thing I did was, I read the Elastic search mock [blog}(https://www.elastic.co/blog/smooth-mocking-with-the-elasticsearch-node-js-client). It has a lot of information. First thing I try was I use the client
and the mock
from the Elasticsearch
library. Example:
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' }
})
I have pass the mock.getConnection()
to the Connection
for client. From this, all of the connection is going through the mock Elasticsearch instead of the real Elasticsearch. Then I add a mock. It requires the method and paths and returns a status. After ward I use the supertest
library to request the first mock created.
const res = await request(app).get('/');
expect(res.status).toBe(200);
When I ran this test I was expecting a status 200
, but I got was an 400
. I try to add paths to the mock to see if I can get results.
mock.add({
method: 'GET',
path: '/test'
}, () => {
return { status: 'ok' }
})
I still got the 400
status.
correct way of connecting
After talking to Roxanne, I found out what I did wrong. Firstly, when I add my mock, the method
and path
was wrong. My method was missing the POST
.
method: ['POST', 'GET'],
Secondly, my path was incorrect. It should not be use as a path parameters, instead it should've been a query parameters.
path: '/posts/post/_search',
The query format is found in the search.js
file.
const {
body: { hits },
} = await client.search({
from: calculateFrom(page, perPage),
size: perPage,
_source: ['id'],
index,
type,
body: query,
});
The reason why it's /posts/post/_search
because from the search.js
the variable are index = 'posts'
and type='post'
. If the path was not correctly call exactly, the test will return 400
.
The request now need the query function.
const res = await request(app).get('/').query({ text: 'Telescope', filter: 'post' });
expect(res.statusCode).toBe(200);
I test run the test and it finally return status 200
.
Conclusion
I learn that the setup for mock Elasticsearch has to be very exact, anything wrong will results in a status 400. It was frustrating trying to understand what I did wrong even though it was a very simple fix.
Top comments (0)
Some comments may only be visible to logged-in visitors. Sign in to view all comments.