-
Notifications
You must be signed in to change notification settings - Fork 65
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
How to test NextJS endpoints #84
Comments
These two can help https://www.npmjs.com/package/light-my-request or https://www.npmjs.com/package/supertest Using the above, you don't have to start and close the server. |
Thanks for the reply! I tried setting up a test for the file mentioned above, and realize it's the database and session middlewares causing the import nextConnect from 'next-connect';
import middleware from '../../../../middlewares/middleware';
import { getUser } from '../../../../lib/db';
const handler = nextConnect();
handler.use(middleware);
// dumb API without mongo
handler.get(async (req, res) => {
res.send(`hello ${req.query.userId}`);
});
export default handler; // working test, assuming no Mongo middleware applied
import http from 'http'
import fetch from 'isomorphic-unfetch'
import listen from 'test-listen'
import { apiResolver } from 'next/dist/next-server/server/api-utils'
import handler from '.';
const USER_ID = 'SOME_ID' // to be an actual id once 500 is resolved
describe(`/users/${USER_ID}`, () => {
test('responds 200', async () => {
const requestHandler = (req, res) => apiResolver(req, res, undefined, handler)
const server = http.createServer(requestHandler)
const url = await listen(server)
const response = await fetch(`${url}/${USER_ID}`)
expect(response.status).toBe(200)
return server.close()
})
}) Seems like this issue doesn't pertain to this repo, but more towards the |
Nope, wrong assumption, you are using |
Still getting a 500 sadly. Here are the changes I made if you want to see what's going on: https://github.com/yegorkay/nextjs-mongodb-app/tree/feature/testing |
@yegorkay hey sorry I didn't check in here for a while - I have a whole testing setup tailored for nextjs here: and a few tests: https://github.com/ljosberinn/personal-react-boilerplate/tree/master/src/server/__tests__ |
My approach to test is https://github.com/leosuncin/literate/blob/master/tests/pages/api/auth/register.spec.ts |
Another idea is to use supertest import http from 'http';
import { apiResolver } from 'next/dist/next-server/server/api-utils';
import supertest from 'supertest';
import registerHandler from 'pages/api/auth/register';
jest.setTimeout(10e3);
describe('[POST] /api/auth/register', () => {
let server;
beforeEach(async () => {
const requestHandle = (request, response) =>
apiResolver(
request,
response,
undefined,
registerHandler,
{},
true,
);
server = http.createServer(requestHandler);
});
afterEach(() => {
server.close();
});
it('creates a new user', async () => {
const body = {
name: 'John Doe',
email: 'johndoe@example.com',
password: 'ji32k7au4a83',
};
const result = await supertest(server)
.post('/api/auth/register')
.send(body)
.expect(201)
.expect('Content-Type', /json/);
expect(result.body).toBeDefined();
});
it.each([
{
name: '',
email: '',
password: '',
},
{
name: 'a',
email: 'email',
password: '123456',
},
])('validate the body %p', async (body) => {
const result = await supertest(server)
.post('/api/auth/register')
.send(body)
.expect(422)
.expect('Content-Type', /json/);
expect(result.body).toBeDefined();
});
}); It has the advantage of no need to start a server |
Is there a way to test with Jest? This isn't my post but it is the same issue I'm facing: https://stackoverflow.com/questions/67961149/how-do-i-test-a-next-js-api-route-which-uses-next-connect When using Cheers! |
@nath-green I use next-connect and using leosuncin's solution worked for me. It no longer has the jest timeout issue |
If someone need some guidance about how to write unit-tests (with Jest) for your middlewares, I've written a blog-post about it. If you need to test an API route endpoint from end-to-end, I recommend using next-test-api-route-handler (ntarh) |
I suggest you to use this pacakge: https://www.npmjs.com/package/nextjs-http-supertest. |
How is it better against ntarh? ntarh seems to be already established solution for Next.js endpoint unit-testing. It takes cares of all the boring unit-testing setup and doesn't use an HTTP server. It directly executes the endpoint function which seems to be more efficient and performant. |
As you said, ntarh is a solution for unit-testing. In unit testing you are only verifying that you're handler is working well, but you don't know if the correct handler is reach for a given endpoint. |
Looking at your other repo, how would you go about testing an endpoint that uses middleware, or endpoints, in general, using
next-connect
?https://github.com/hoangvvo/nextjs-mongodb-app/blob/master/pages/api/users/%5BuserId%5D/index.js#L8
A potential test:
Doing something like this returns a 500. Maybe it's just me who is lacking testing knowledge. Any help would be appreciated.
The text was updated successfully, but these errors were encountered: