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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mocking of the comminucation #33

Open
kdybicz opened this issue Apr 1, 2022 · 2 comments
Open

Mocking of the comminucation #33

kdybicz opened this issue Apr 1, 2022 · 2 comments

Comments

@kdybicz
Copy link

kdybicz commented Apr 1, 2022

I'm trying to replace apollo-datasource-rest with your library, but I'm struggling with mocking of the external communication:

import { MockAgent, setGlobalDispatcher } from 'undici';

const mockAgent = new MockAgent();
setGlobalDispatcher(mockAgent);
mockAgent.disableNetConnect();
...
const mockPool = mockAgent.get(BASE_URL);
...
mockPool.intercept({ path: '/v1/something', method: 'GET' }).reply(404, '');
...
const api = new MyAPI();
const result = await api.doSomething('aaa', 'bbb');

Above is failing because DataSource is successfully reaching to the unmocked REST resource and gets rejected with HTTP 401. I don't find mocking you use in your tests as best approach and would like to make the undici mocking work with apollo-datasource-http, though I might need some help with that. Any suggestions?

@dmateiu
Copy link

dmateiu commented Jun 23, 2022

@kdybicz I had the very same issue and was able to fix this, after stumbling across another issue in the undici repo, see nodejs/undici#996.

The MockAgent does not intercept Pool requests, instead you need to injection the agent during tests.

For your tests to work, you would therefore need to adapt the constructor on your MyAPI class to accept the mock client as a parameter.

// my-api.data-source.ts

class MyAPI extends HTTPDataSource {
  constructor(pool: Pool) { // <-- Accept Pool here
    super(baseUrl, {
      pool,
    })
  }
}
// my-api.data-source.test.ts

import type { MockClient } from 'undici';
import { MockAgent, setGlobalDispatcher } from 'undici';

const mockAgent = new MockAgent();
setGlobalDispatcher(mockAgent);
mockAgent.disableNetConnect();

const mockPool = mockAgent.get<MockClient>(BASE_URL);
mockPool.intercept({ path: '/v1/something', method: 'GET' }).reply(404, '');

const api = new MyAPI(mockPool); // <-- Inject mock client here

const result = await api.doSomething('aaa', 'bbb');

I hope this solves your problem.

@kdybicz
Copy link
Author

kdybicz commented Jun 23, 2022

@dmateiu Thank You for your reply, looks like you're right and I think this might be a use case example for docs! I till need to sort out ie. if I can ensure proper timeout was set on my request, etc. but this for sure unblocks future work!

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