supertest
supertest copied to clipboard
[fix] Next.js: Ubuntu 22.04.3 LTS
Describe the bug
Node.js version: v20.9.0
OS version: Ubuntu 22.04.3 LTS
Description:
Next.js v"13.4.0"
When running jest + supertest simple API end point test we get the following error.
"TypeError: res.status is not a function" when running
Actual behavior
npm run test
> [email protected] dev
> next dev
ready - started server on 0.0.0.0:3000, url: http://localhost:3000
info - Loaded env from /home/ken/Programming/next-mtrade/.env
event - compiled client and server successfully in 456 ms (264 modules)
^C
ken@kmb:~/Programming/next-mtrade$ npm run test
> [email protected] test
> jest
FAIL __tests__/api/hello.spec.ts (11.442 s)
API Route - /api/hello
✕ responds with "Hello, World" (10002 ms)
● API Route - /api/hello › responds with "Hello, World"
TypeError: res.status is not a function
9 | res: NextApiResponse<ResponseData>
10 | ) {
> 11 | res.status(200).json({ message: "Hello from Next.js!" });
| ^
12 | }
13 |
at Server.<anonymous> (src/pages/api/hello.ts:11:9)
at step (src/pages/api/hello.ts:33:23)
at Object.next (src/pages/api/hello.ts:14:53)
at src/pages/api/hello.ts:8:71
at Object.<anonymous>.__awaiter (src/pages/api/hello.ts:4:12)
at Server.hello (src/pages/api/hello.ts:40:12)
● API Route - /api/hello › responds with "Hello, World"
thrown: "Exceeded timeout of 10000 ms for a test.
Add a timeout value to this test to increase the timeout, if this is a long-running test. See https://jestjs.io/docs/api#testname-fn-timeout."
4 |
5 | describe("API Route - /api/hello", () => {
> 6 | it('responds with "Hello, World"', async () => {
| ^
7 | const response = await request(hello).get("/api/hello");
8 |
9 | expect(response.status).toBe(200);
at __tests__/api/hello.spec.ts:6:5
at Object.<anonymous> (__tests__/api/hello.spec.ts:5:1)
Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 total
Snapshots: 0 total
Time: 11.483 s, estimated 13 s
Ran all test suites.
Jest did not exit one second after the test run has completed.
'This usually means that there are asynchronous operations that weren't stopped in your tests. Consider running Jest with `--detectOpenHandles` to troubleshoot this issue.
Expected behavior
Jest + Supertest API end point test should pass
We except a status code of 200 and a json message containgin "{"message":"Hello from Next.js!"}"
Code to reproduce
// __tests__/api/hello.spec.ts
import request from "supertest";
import hello from "../../src/pages/api/hello";
describe("API Route - /api/hello", () => {
it('responds with "Hello, World"', async () => {
const response = await request(hello).get("/api/hello");
expect(response.status).toBe(200);
expect(response.body).toEqual({ message: "Hello, World" });
}, 10000); // Set a 10-second timeout
});
//src/pages/api/hello.ts
import type { NextApiRequest, NextApiResponse } from "next";
type ResponseData = {
message: string;
};
export default async function hello(
req: NextApiRequest,
res: NextApiResponse<ResponseData>
) {
res.status(200).json({ message: "Hello from Next.js!" });
}
My repo https://github.com/kbventures/next-mtrade
Checklist
- [ x] I have searched through GitHub issues for similar issues.
Similiar issue for express.js
- [ x] I have completely read through the README and documentation.
- [x ] I have tested my code with the latest version of Node.js and this package and confirmed it is still not working.