Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
pvlastaridis opened this issue Sep 17, 2024 · 2 comments
Open
Labels
question Further information is requested

Comments

@pvlastaridis
Copy link

pvlastaridis commented Sep 17, 2024

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?

@pvlastaridis pvlastaridis changed the title I want to test my server api endpoint and mock useRequireSession but I cannot get to work I want to test my server api endpoint and mock useRequireSession but I cannot get it to work Sep 17, 2024
@pvlastaridis pvlastaridis changed the title I want to test my server api endpoint and mock useRequireSession but I cannot get it to work I want to test my server api endpoint and mock useRequireSession Sep 17, 2024
@acidjazz
Copy link
Contributor

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
Copy link
Author

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!!

@atinux atinux added the question Further information is requested label Sep 25, 2024 — with Volta.net
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants