1.0.2 • Published 10 months ago

vitest-response-matchers v1.0.2

Weekly downloads
-
License
-
Repository
-
Last release
10 months ago

Vitest Response Matchers

A vitest matchers library for validating HTTP responses using a simple and intuitive syntax.

Installation

npm install --save-dev vitest-response-matchers
yarn add -D vitest-response-matchers
pnpm add -D vitest-response-matchers

Then create a setup.ts (or setup.js if using Javascript) file and extend the matchers.

import { expect } from "vitest";
import { responseMatchers } from "vitest-response-matchers";

expect.extend(responseMatchers);

Example

import { describe, it, expect } from "vitest";

describe("GET /users/:id", () => {
  it("should pass if a valid user id is given", async () => {
    const response = await fetch("/api/users/1");

    // assert response to have status 200
    await expect(response).toHaveStatusOk();

    // assert response json structure with arrays
    await expect(response).toHaveJsonStructure([
      "name",
      "surname",
      "email",
      ["role", ["id", "name", ["permissions*", ["id", "name"]]]],
    ]);

    // Or with yaml sintax
    await expect(response).toHaveJsonStructure(`
            - name
            - surname
            - email
            - role:
                - id
                - name
                - permissions*:
                    - id
                    - name
        `);
  });
});
import { describe, it, expect } from "vitest";

describe("POST /auth/login", () => {
  it("should fail if credentials are not provided", async () => {
    const response = await fetch("/api/auth/login", {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
      },
      body: {},
    });

    // assert response to not have status 200
    await expect(response).not.toHaveStatusOk();

    // assert response to have status 401
    await expect(response).toHaveStatusUnauthorized();
});

Read the docs for the full list of matchers.

1.0.2

10 months ago

1.0.1

10 months ago

1.0.0

10 months ago