Jest: RESTfull

//[zipcode].ts
import type { NextApiRequest, NextApiResponse } from "next";
import { findByZip } from "@/mongoose/weather/services";
import dbConnect from "@/middleware/db-connect";

dbConnect();


export default async function handler(
    req: NextApiRequest,
    res: NextApiResponse
): Promise<NextApiResponse<WeatherDetailType> | void> {
    let data = await findByZip(req.query.zipcode as string);
    return res.status(200).json(data);
}
//zipcode.e2e.test.ts
describe("The API /v1/weather/[zipcode]", () => {
    test("returns the correct data from the zipcode 96815", async () => {
        const zip = "96815";
        let response = await fetch(`http://localhost:3002/api/v1/weather/${zip}`);
        let body = await response.json();
        expect(body.zip).toEqual(zip);
    })
})

訂閱 Winerva 電子報

工廠資料收集、視覺辨識與 AI 應用的實務文章。

送出後請到信箱按確認連結才算訂閱。您的 Email 只用來寄送 Winerva 的文章與技術消息,每封信都可以取消訂閱。

Spread the love