Created
October 4, 2023 13:11
-
-
Save william-murphy/01c6e597fc0df7f27df7c020635fdfac to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ~/tests/api/thing.test.ts | |
import 'isomorphic-fetch' // because otherwise NextRequest and NextResponse aren't defined?? | |
import { createRequest, createResponse } from "node-mocks-http" | |
import { GET, POST } from "~/app/api/thing/route" | |
import type { Thing } from "~/types/thing" | |
descrive("/api/thing", () => { | |
test("creates a new thing with valid data", async () => { | |
const thing: Thing = { | |
title: "test title", | |
purpose: "test purpose" | |
} | |
const req = createRequest({ | |
method: "POST", | |
url: "/api/thing", | |
headers: { | |
"Content-Type": "application/json" | |
}, | |
body: thing | |
} | |
const res = await POST(req) // at this point it goes to the route handler function, never to return... | |
// the rest of the code | |
}) | |
}) | |
// ~/app/api/thing/route.ts | |
import { NextRequest, NextResponse } from "next/server" | |
export async function POST(request: NextRequest) { | |
const { title, purpose } = await request.json() | |
// at this point (line 33) it errors with request.json is not a function, however, when I test the routes manually with | |
// npm run dev and hitting them with postman this works fine | |
// rest of the code that doesn't get reached during the test... | |
} |
sabry-awad97
commented
Nov 15, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment