Skip to content

Commit

Permalink
Merge pull request #51 from lifeomic/mock-headers
Browse files Browse the repository at this point in the history
feat: support mocking response headers
  • Loading branch information
swain authored Jul 24, 2024
2 parents 72fdca4 + 4647b97 commit e6579ad
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 12 deletions.
38 changes: 38 additions & 0 deletions src/test-utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,44 @@ describe('createAPIMockingUtility', () => {
expect(result.status).toStrictEqual(200);
expect(result.data).toStrictEqual({ message: payload.message });
});

test('static response with headers', async () => {
network.mock('GET /items/:id', {
status: 200,
data: { message: 'some-message' },
headers: {
'test-response-header': 'some-value',
},
});

const payload = { id: v4(), filter: v4() };
const result = await client.request('GET /items/:id', payload);

expect(result.status).toStrictEqual(200);
expect(result.data).toStrictEqual({ message: 'some-message' });
expect(result.headers).toMatchObject({
'test-response-header': 'some-value',
});
});

test('function response with headers', async () => {
network.mock('GET /items/:id', () => ({
status: 200,
data: { message: 'some-message' },
headers: {
'test-response-header': 'some-value',
},
}));

const payload = { id: v4(), filter: v4() };
const result = await client.request('GET /items/:id', payload);

expect(result.status).toStrictEqual(200);
expect(result.data).toStrictEqual({ message: 'some-message' });
expect(result.headers).toMatchObject({
'test-response-header': 'some-value',
});
});
});

test('mockOrdered', async () => {
Expand Down
38 changes: 26 additions & 12 deletions src/test-utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { RequestHandler, rest } from 'msw';
import { RequestHandler, ResponseTransformer, rest } from 'msw';
/**
* Important: we need to avoid import actual values from these imports:
* - 'msw/node'
Expand All @@ -20,8 +20,16 @@ type MSWUsable = {
};

export type APIMockerResponse<T> =
| { status: 200; data: T }
| { status: 400 | 401 | 403 | 404 | 500; data: any };
| {
status: 200;
data: T;
headers?: Record<string, string>;
}
| {
status: 400 | 401 | 403 | 404 | 500;
data: any;
headers?: Record<string, string>;
};

type MockRequestHandler<
Endpoints extends RoughEndpoints,
Expand Down Expand Up @@ -112,13 +120,22 @@ export const createAPIMocker = <Endpoints extends RoughEndpoints>(

server.use(
rest[lowercaseMethod](`${baseUrl}${url}`, async (req, res, ctx) => {
const resolve = options.once ? res.once : res;
const toMSWResponse = (response: APIMockerResponse<any>) => {
const resolve = options.once ? res.once : res;
const transformers: ResponseTransformer[] = [
ctx.status(response.status),
ctx.json(response.data),
];
if (response.headers) {
for (const [key, value] of Object.entries(response.headers)) {
transformers.push(ctx.set(key, value));
}
}
return resolve(...transformers);
};

if (typeof handlerOrResponse !== 'function') {
return resolve(
ctx.status(handlerOrResponse.status),
ctx.json(handlerOrResponse.data),
);
return toMSWResponse(handlerOrResponse);
}

const mockRequest = {
Expand Down Expand Up @@ -151,10 +168,7 @@ export const createAPIMocker = <Endpoints extends RoughEndpoints>(
});
}

return resolve(
ctx.status(mockedResponse.status),
ctx.json(mockedResponse.data),
);
return toMSWResponse(mockedResponse);
}),
);

Expand Down

0 comments on commit e6579ad

Please sign in to comment.