Files
vercel/packages/edge/test/response.test.ts
Damien Simonin Feugas 61e588cd63 feat(edge): adds json() response helper (#9081)
### 🔖 What's in there?

Because Typescript's `libdom` does not have [`static Response.json()`](https://github.com/microsoft/TypeScript-DOM-lib-generator/issues/1444) (which modern browsers and edge runtimes are supporting), Typescript users can't use easily use it.

This helper fills the gap.

### 🧪 How to test?

It's covered with unit tests
2022-12-15 11:01:02 +00:00

35 lines
1.0 KiB
TypeScript
Vendored

/**
* @jest-environment @edge-runtime/jest-environment
*/
import { json } from '../src/response';
describe('json', () => {
it('returns a response with JSON content', async () => {
const content = { foo: 'bar' };
const response = json(content);
expect(response.headers.get('content-type')).toEqual('application/json');
expect(await response.json()).toEqual(content);
});
it('can set response init', async () => {
const content = { bar: 'baz' };
const status = 201;
const statusText = 'it is in';
const customHeader = 'x-custom';
const customHeaderValue = '1';
const response = json(content, {
status,
statusText,
headers: { [customHeader]: customHeaderValue },
});
expect(response).toMatchObject({
status,
statusText,
});
expect(response.headers.get('content-type')).toEqual('application/json');
expect(response.headers.get(customHeader)).toEqual(customHeaderValue);
expect(await response.json()).toEqual(content);
});
});