DEV Community

Jia Hua Zou
Jia Hua Zou

Posted on

Search test pt.1

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.


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.


First thing I did was, I read the Elastic search mock [blog}( 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() 
  method: 'GET', 
  path: '/' 
}, () => { 
  return { status: 'ok' } 
Enter fullscreen mode Exit fullscreen mode

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('/');
Enter fullscreen mode Exit fullscreen mode

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.

  method: 'GET', 
  path: '/test' 
}, () => { 
  return { status: 'ok' } 
Enter fullscreen mode Exit fullscreen mode

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'],
Enter fullscreen mode Exit fullscreen mode

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',
Enter fullscreen mode Exit fullscreen mode

The query format is found in the search.js file.

  const {
    body: { hits },
  } = await{
    from: calculateFrom(page, perPage),
    size: perPage,
    _source: ['id'],
    body: query,
Enter fullscreen mode Exit fullscreen mode

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' });
Enter fullscreen mode Exit fullscreen mode

I test run the test and it finally return status 200.


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)