atinux / nuxt-auth-utils

Add Authentication to Nuxt applications with secured & sealed cookies sessions.
MIT License
974 stars 91 forks source link

I want to test my server api endpoint and mock useRequireSession #182

Open pvlastaridis opened 2 months ago

pvlastaridis commented 2 months ago

I want to test my server api endpoint and mock useRequireSession but I cannot get it to work

I am trying something like this

// @vitest-environment nuxt
import { describe, it, expect, vi } from "vitest";
import { createEvent } from "h3";
import { IncomingMessage, ServerResponse } from "http";
import { Socket } from "net";
import userHandler from "~/server/api/profile/index.get";

// Mock the module
vi.mock("nuxt-auth-utils", () => ({
  requireUserSession: vi.fn()
}));

describe("API - /api/profile", () => {
  it("should return user data if session exists", async () => {
    // TypeScript may not recognize this, so use @ts-ignore if necessary
    // @ts-ignore
    const { requireUserSession } = await import("nuxt-auth-utils");
    requireUserSession.mockResolvedValue({
      user: {
        id: 1,
        username: "name",
        roles: ["ROLE_1", "ROLE_2"]
      }
    });

    // Create a mock socket and request/response objects
    const socket = new Socket();
    const req = new IncomingMessage(socket);
    const res = new ServerResponse(req);

    req.url = "/api/profile";
    req.method = "GET";

    const event = createEvent(req, res);

    const response = await userHandler(event);

    console.log(response, "res");

    expect(requireUserSession).toHaveBeenCalledTimes(1);
  });

But it is not working as in my endpoint the problem is ReferenceError: requireUserSession is not defined ❯ Module.default server/api/profile/index.get.ts:6:19 and I cannot import requireUserSession in my endpoint as when I type

import requireUserSession it says that it need a second argument of type Nuxt?

Can someone please help me?

acidjazz commented 2 months ago

i setup my tests w/out any mocking, here are some i have in this branch of a project:

https://github.com/fumeapp/bio/blob/cycles/test/user.test.ts https://github.com/fumeapp/bio/blob/cycles/test/round.test.ts

helpers in the tests found here:

https://github.com/fumeapp/bio/blob/cycles/test/auth.ts

here they are passing in an action:

https://github.com/fumeapp/bio/actions/runs/10898368283/job/30241453762

image

i plan to try and move some of this in its own package at some point

pvlastaridis commented 2 months ago

i setup my tests w/out any mocking, here are some i have in this branch of a project:

https://github.com/fumeapp/bio/blob/cycles/test/user.test.ts https://github.com/fumeapp/bio/blob/cycles/test/round.test.ts

helpers in the tests found here:

https://github.com/fumeapp/bio/blob/cycles/test/auth.ts

here they are passing in an action:

https://github.com/fumeapp/bio/actions/runs/10898368283/job/30241453762

image

i plan to try and move some of this in its own package at some point

This is great work thank so much for sharing!!