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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

undici mocks are not picked up by elasticsearch-client #1674

Closed
djmadeira opened this issue Apr 4, 2022 · 4 comments
Closed

undici mocks are not picked up by elasticsearch-client #1674

djmadeira opened this issue Apr 4, 2022 · 4 comments

Comments

@djmadeira
Copy link

馃悰 Bug Report

When using undici's MockAgent to mock requests, the elastic client does not pick up any of the mocks specified. I understand I could also use elasticsearch-mock, and for the most part I do, but in my tests that validate initializing the client is working, I want to test against actual HTTP requests. So this is a desirable feature to suppport

To Reproduce

Jest test which demonstrates the issue:

const { setGlobalDispatcher, MockAgent } = require('undici');
const { Client } = require('@elastic/elasticsearch');
const { request } = require('undici');

const mockAgent = new MockAgent();

beforeAll(() => {
  mockAgent.disableNetConnect();
  setGlobalDispatcher(mockAgent);
});

const response = {
  '_index': 'test-index',
  '_type': '_doc',
  '_id': 'TEST_ID',
  '_version': 1,
  '_seq_no': 0,
  '_primary_term': 1,
  'found': true,
  '_source': {}
};

test('undici', async () => {
  mockAgent.get('http://test-cluster:9200')
    .intercept({ path: '/test-index/_doc/TEST_ID', method: 'GET' })
    .reply(200, response);

  const { body } = await request('http://test-cluster:9200/test-index/_doc/TEST_ID');

  expect(await body.json()).toEqual(response);
});

test('elasticsearch-client', async () => {
  mockAgent.get('http://test-cluster:9200')
    .intercept({ path: '/test-index/_doc/TEST_ID', method: 'GET' })
    .reply(200, response);

  const client = new Client({
    node: 'http://test-cluster:9200'
  });

  // Throws ConnectionError: getaddrinfo ENOTFOUND test-cluster
  const result = await client.get({
    index: 'test-index',
    id: 'TEST_ID'
  });

  expect(result).toEqual(response);
});

Expected behavior

elasticsearch-client should respect the Agent set by setGlobalDispatcher, but it does not.

Your Environment

  • node version: 16
  • @elastic/elasticsearch version: >=8.0.0
  • os: Mac
@delvedor
Copy link
Member

Confirmed, currently working on a fix.

@delvedor
Copy link
Member

delvedor commented Apr 12, 2022

I've opened elastic/elastic-transport-js#48 which will solve your issue.
The tl;dr is that the client is using the low-level API of undici, and the global dispatcher won't be picked up. The only solution for now is to allow the user to pass the custom mock agent. I'm chatting with the undici core team to understand if we can improve this before releasing our fix.

Meanwhile, I recommend you checkout @elastic/elasticsearch-mock, a mocking utility designed for this library.

@delvedor
Copy link
Member

delvedor commented May 6, 2022

Hello! With nodejs/undici#1405 your issue should be solved! :)

@delvedor delvedor closed this as completed May 6, 2022
@djmadeira
Copy link
Author

@delvedor the test case above still doesn't work on 8.2.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants