Express integration tests

PHOTO EMBED

Thu Oct 07 2021 04:42:41 GMT+0000 (Coordinated Universal Time)

Saved by @rook12 #javascript

const request = require("supertest");
const app = require("./app");

describe("app.get", () => {
  test("get /properties should return the list of properties and 200 status", async () => {
    const response = await request(app).get("/properties");

    expect(response.statusCode).toBe(200);
    expect(response.body).toEqual(
      expect.arrayContaining([
        expect.objectContaining({
          askingPrice: expect.any(String),
          description: expect.any(String),
          address: expect.any(String),
          title: expect.any(String),
          img: expect.any(String),
          id: expect.any(String),
        }),
      ])
    );
  });

  test("get /properties/:id should return a single property and 200 status", async () => {
    const response = await request(app).get(
      "/properties/61480db44ab0cf7175467757"
    );

    expect(response.statusCode).toBe(200);
    expect(response.body).toEqual(
      expect.objectContaining({
        askingPrice: expect.any(String),
        description: expect.any(String),
        address: expect.any(String),
        title: expect.any(String),
        img: expect.any(String),
        id: expect.any(String),
      })
    );
  });

  test("get /properties/invalid-id should return a 400 status and custom message", async () => {
    const response = await request(app).get("/properties/invalid-id");

    expect(response.statusCode).toBe(400);
    expect(response.body).toEqual({ message: "id provided is invalid" });
  });

  test("get /properties/valid-but-not-found-id should return a 404 status and custom message", async () => {
    const response = await request(app).get(
      "/properties/61480db44ab0cf7175467717"
    );

    expect(response.statusCode).toBe(404);
    expect(response.body).toEqual({ message: "id not found" });
  });
});

describe("app.post", () => {
  test("post /properties should create a new property", async () => {
    const body = {
      askingPrice: "$1",
      description: "house",
      address: "1 house street",
      title: "cool house",
      img: "https://placeimg.com/642/482/arch",
    };
    const response = await request(app).post("/properties").send(body);

    expect(response.statusCode).toBe(200);
    expect(response.body).toEqual(
      expect.objectContaining({ ...body, id: expect.any(String) })
    );
  });
});
content_copyCOPY