-
Notifications
You must be signed in to change notification settings - Fork 34
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
Support for node-fetch #251
Comments
Hi @rmclaughlin-nelnet, thanks for raising this! 😃 I've been meaning to support What's the error you get when passing a I expect the solution will be straightforward:
I'll do this when I have time, if you want it sooner you're welcome to make a pull request |
Thanks for the response. Here is the error I get
|
Yes, we are on 2.6.x |
hey, so Problem Firstly there is no sync method to access the node-fetch response body. It must be accessed via either Secondly, the response does not include the original request method. We use this to find which OpenAPI response definition the actual Workaround (corrected on 30 Oct) describe('test', () => {
it('passes', async () => {
const rawResponse = await fetch('endpoint');
const body = await rawResponse.json();
const res = {
req: {
path: rawResponse.url,
method: 'GET',
},
status: rawResponse.status,
body,
text: JSON.stringify(body),
};
expect(res).to.satisfyApiSpec;
});
}); If you need to do lots of these, you can automate the parsing: type HTTPMethod =
| 'GET'
| 'POST'
| 'PUT'
| 'DELETE'
| 'OPTIONS'
| 'HEAD'
| 'PATCH';
type ParsedResponse = {
req: {
path: string;
method: HTTPMethod;
};
status: number;
body: unknown;
text: string;
};
const parseResponse = async (
rawResponse: NodeFetchResponse,
requestMethod: HTTPMethod,
): Promise<ParsedResponse> => {
const isJson = rawResponse.headers.get('content-type')?.includes('json');
const req = {
path: rawResponse.url,
method: requestMethod,
};
const { status } = rawResponse;
if (isJson) {
const body = await rawResponse.json();
const text = JSON.stringify(body);
return {
req,
status,
body,
text,
};
}
const text = await rawResponse.text();
return {
req,
status,
body: {},
text,
};
};
describe('test', () => {
it('passes', async () => {
const rawResponse = await fetch('endpoint');
const res = await parseResponse(rawResponse, 'GET');
expect(res).to.satisfyApiSpec;
});
}); Longer term solution describe('test', () => {
it('passes', async () => {
const method = 'GET';
const res = await fetch('endpoint', { method });
await expect(res).toEventuallySatisfyApiSpec(method);
});
}); Or, expose the above |
Thanks for looking into it. the first solution should be good enough for me, but I agree a longer term solution would be better. I was trying out the first solution, but I ran into problems. Here is what I did, slightly different from what you typed out.
|
Ah, sorry I miswrote the first solution slightly, you need the I've corrected the above example, so it should work when a response header specifies the content type is json. If instead the content type is text, use this: describe('test', () => {
it('passes', async () => {
const rawResponse = await fetch('endpoint');
const text = await rawResponse.text();
const res = {
req: {
path: rawResponse.url,
method: 'GET',
},
status: rawResponse.status,
body: {},
text,
};
expect(res).to.satisfyApiSpec;
});
}); Let me know if these work, and we can find a better long-term solution |
Ok, I got it to work with a few tweaks const rawResponse = await fetch('endpoint');
const body = await rawResponse.json();
const res = {
req: {
path: rawResponse.url,
method: 'GET',
},
status: rawResponse.status,
body,
};
expect(res).toSatisfyApiSpec();
|
Thanks! In answer to your points:
|
OpenAPI version
2 and/or 3?
3
Is your feature request related to a problem? Please describe.
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
This looks like a great solution to a problem we have been facing. However, we use node-fetch exclusively in our code and tests. Do you have any plans to support node-fetch?
Describe the solution you'd like
A clear and concise description of what you want to happen.
Support for node-fetch
Describe alternatives you've considered
A clear and concise description of any alternative solutions or features you've considered.
Additional context
Add any other context or screenshots about the feature request here.
Are you going to resolve the issue?
The text was updated successfully, but these errors were encountered: