If you haven't read my part 1, Link here. TLDR, it's all about the connection to a mock Elasticsearch and the problems I faced.
Tests
This time it's all about the testing that I have created (Github PR). So creating the test was very easy once I got the mock connection working. My focus for these are makin sure query are sending the proper text and receiving proper data. The first setup I did was to create the describe
for the normal search and then I put in all of the individual tests with it
function like this:
describe('/query routers', () => {
it('return error 400 if no params given', async () => {
const res = await request(app).get('/');
expect(res.status).toBe(400);
expect(res.statusCode).toBe(400);
});
});
This first test was create before(Link to blog) when I setup the initial testing. Before adding different test, I have to make sure the mock has to be clear before each tests. To do that, jest has a function that can easy perform this like so:
afterEach(() => {
mock.clearAll();
});
Testing for Statuscode
After that, my first few test will be related to anything with the status code 400
or 503
. To cause the status code 400
, the query send will have mistakes. So the query filter
has to be either post
or author
anything else will cause a status 400
. Also if the text
query is empty it will also return the same error. code block.
returns for status code 400
() => {
return {};
}
//proper return
() => {
return {
results: 0,
hits: {
total: { value: 0 },
hits: [],
},
};
}
If your wondering why I have it return nothing and not return with a proper format(returning results
and hits
), it because the query isn't proper sent correctly, the result values will nothing but an error.
If the query was sent correctly, and it returns not a proper format it will cause a error 503
because the search results is expecting a results
and hits
.Error 503 test. request format
Testing for results
After testing the error 400
and 503
now I can start testing for results. There are only 2 tests, one for post
and the other for author
. Tests for them. So for these tests, I made sure that I can expect a status code 200
with results and total number of results.
This part was a little tricky because if any of the format is wrong, it will cause a status code 503
. The return must have a results
with a hits
. Within that hits
it will display the total
result values with a list of url
link with the id
. This is the format that it needs to return.
() => {
return {
results: 2,
hits: {
total: { value: 2 },
hits: [
{
_id: '1234',
url: `${POSTS_URL}/`,
},
{
_id: '5678',
url: `${POSTS_URL}/`,
},
],
},
};
Conclusion
Tackling this issue felt it was hard and easy. Mostly the mock Elasticsearch connection was the most challenging for me. It did felt very rewarding when I was able to merge my work to the repo. After this I felt like I can do more issues related to Elasticsearch
.
Top comments (0)
Some comments may only be visible to logged-in visitors. Sign in to view all comments.