elasticsearch-js-mock icon indicating copy to clipboard operation
elasticsearch-js-mock copied to clipboard

elastic mocks timeout when used with jest fake timers

Open mouhannad-sh opened this issue 2 years ago • 0 comments

Hi there 👋🏻

Elasticsearch mocks don't work with jest fake timers and the jest test runner seems to just time out when I use fake timers

To reproduce the issue please check this codesandbox

// uncomment the following 2 lines and the tests will timeout and fail
// jest.useFakeTimers("modern");
// jest.setSystemTime(new Date());

it("should run", async () => {
  // when using fake timers you can use the following line to get the test to run correctly
  // jest.useRealTimers();
  const { Client } = require("@elastic/elasticsearch");
  const Mock = require("@elastic/elasticsearch-mock");
  const elasticMock = new Mock();
  const client = new Client({
    node: "http://localhost:9200",
    Connection: elasticMock.getConnection()
  });
  elasticMock.add({ method: "GET", path: "/_cat/health" }, () => ({
    status: "ok"
  }));
  const fake = await client.cat.health();
  expect(fake.status).toEqual("ok");
});

We push data to elastic search as a callback / side effect for write operations on some models, some of which are time sensitive... we use fake timers to help us tests different scenarios.

Is there a way where we can use elasticsearch-mock with jest fake timers or any other timer alternatives ?

mouhannad-sh avatar Mar 24 '22 03:03 mouhannad-sh