Replies: 1 comment
-
Hi! You can do it in the same way! The only difference is to pass the import { createMocks } from 'node-mocks-http';
import handleUsers from './[[...params]]';
describe('/api/users/[[...params]]', () => {
test('Fetches a user', async () => {
const { req, res } = createMocks({
method: 'GET',
url: '/api/users/101',
});
await handleUsers(req, res);
expect(res._getStatusCode()).toBe(200);
expect(res._getData()).toEqual(
expect.objectContaining({
id: 101,
}),
);
});
test('Returns 404', async () => {
const { req, res } = createMocks({
method: 'GET',
url: '/api/users/101111',
});
await handleUsers(req, res);
expect(res._getStatusCode()).toBe(404);
expect(JSON.parse(res._getData())).toEqual(
expect.objectContaining({
message: "No user found with ID '101111'.",
}),
);
});
}); |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi! First off, awesome library! My question is re. unit tests - how do you write them? It's not mentioned anywhere in docs.
Previously, I used node-mocks-http to test my NextJS API routes, and it worked. However, it doesn't seem to work in this case. Here is my code:
donations.test.ts
pages/api/donations.ts
Beta Was this translation helpful? Give feedback.
All reactions