Skip to content

Commit

Permalink
refactor(api): replace getSession
Browse files Browse the repository at this point in the history
For no reason, tests are now failing with NextAuth 4.22.0. I came across
this: nextauthjs/next-auth#7199 so I gave it a
try and it worked!
  • Loading branch information
davidlag0 committed Apr 13, 2023
1 parent f1891fb commit cfb6efe
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 23 deletions.
22 changes: 15 additions & 7 deletions __tests__/unit/api/tasks/[taskId].test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import { createMocks } from "node-mocks-http";
import handle from "../../../../pages/api/tasks/[taskId]";
import { getSession } from "next-auth/react";
import { getServerSession } from "next-auth/next";
import { prismaMock } from "../../../../lib/prismaMockSingleton";

jest.mock("next-auth/react");
jest.mock("next-auth/jwt");
jest.mock("next-auth/next", () => {
const originalModule = jest.requireActual("next-auth/next");

return {
__esModule: true,
...originalModule,
getServerSession: jest.fn(),
};
});

const testSession = {
user: {
Expand Down Expand Up @@ -32,7 +40,7 @@ describe("/api/tasks/[taskId]", () => {
},
});

getSession.mockReturnValue(null);
getServerSession.mockReturnValue(null);

await handle(req, res);

Expand All @@ -52,7 +60,7 @@ describe("/api/tasks/[taskId]", () => {
},
});

getSession.mockReturnValue({});
getServerSession.mockReturnValue({});

await handle(req, res);

Expand All @@ -72,7 +80,7 @@ describe("/api/tasks/[taskId]", () => {
},
});

getSession.mockReturnValue(testSession);
getServerSession.mockReturnValue(testSession);

prismaMock.task.findUnique.mockResolvedValue(null);

Expand All @@ -94,7 +102,7 @@ describe("/api/tasks/[taskId]", () => {
},
});

getSession.mockReturnValue(testSession);
getServerSession.mockReturnValue(testSession);

prismaMock.task.findUnique.mockResolvedValue(testTask);

Expand All @@ -115,7 +123,7 @@ describe("/api/tasks/[taskId]", () => {
},
});

getSession.mockReturnValue(testSession);
getServerSession.mockReturnValue(testSession);

prismaMock.task.delete.mockResolvedValue(testTask);

Expand Down
28 changes: 18 additions & 10 deletions __tests__/unit/api/tasks/index.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import { createMocks } from "node-mocks-http";
import handle from "../../../../pages/api/tasks/index";
import { getSession } from "next-auth/react";
import { getServerSession } from "next-auth/next";
import { prismaMock } from "../../../../lib/prismaMockSingleton";

jest.mock("next-auth/react");
jest.mock("next-auth/jwt");
jest.mock("next-auth/next", () => {
const originalModule = jest.requireActual("next-auth/next");

return {
__esModule: true,
...originalModule,
getServerSession: jest.fn(),
};
});

const testSession = {
user: {
Expand All @@ -31,7 +39,7 @@ describe("/api/tasks/", () => {
method: "GET",
});

getSession.mockReturnValue(null);
getServerSession.mockReturnValue(null);

await handle(req, res);

Expand All @@ -48,7 +56,7 @@ describe("/api/tasks/", () => {
method: "UNSUPPORTED",
});

getSession.mockReturnValue({});
getServerSession.mockReturnValue(testSession);

await handle(req, res);

Expand All @@ -66,7 +74,7 @@ describe("/api/tasks/", () => {
body: { name: "Test Task" },
});

getSession.mockReturnValue(testSession);
getServerSession.mockReturnValue(testSession);

prismaMock.task.create.mockResolvedValue(testTask);

Expand All @@ -83,7 +91,7 @@ describe("/api/tasks/", () => {
method: "POST",
});

getSession.mockReturnValue(testSession);
getServerSession.mockReturnValue(testSession);

await handle(req, res);

Expand All @@ -102,7 +110,7 @@ describe("/api/tasks/", () => {
body: { "other data": "some data" },
});

getSession.mockReturnValue(testSession);
getServerSession.mockReturnValue(testSession);

await handle(req, res);

Expand All @@ -120,7 +128,7 @@ describe("/api/tasks/", () => {
method: "GET",
});

getSession.mockReturnValue(testSession);
getServerSession.mockReturnValue(testSession);

prismaMock.task.findMany.mockResolvedValue([]);

Expand All @@ -139,7 +147,7 @@ describe("/api/tasks/", () => {
method: "GET",
});

getSession.mockReturnValue(testSession);
getServerSession.mockReturnValue(testSession);

prismaMock.task.findMany.mockResolvedValue([testTask]);

Expand All @@ -156,7 +164,7 @@ describe("/api/tasks/", () => {
method: "GET",
});

getSession.mockReturnValue(testSession);
getServerSession.mockReturnValue(testSession);

prismaMock.task.findMany.mockResolvedValue([testTask, testTask2]);

Expand Down
2 changes: 2 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ const customJestConfig = {
"^jose$": require.resolve("jose"),
"^@panva/hkdf$": require.resolve("@panva/hkdf"),
"^uuid$": require.resolve("uuid"),
"^preact-render-to-string$": require.resolve("preact-render-to-string"),
"^preact$": require.resolve("preact"),
},
testPathIgnorePatterns: ["<rootDir>/cypress/"],
};
Expand Down
6 changes: 4 additions & 2 deletions pages/api/auth/[...nextauth].js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import NextAuth from "next-auth";
import GithubProvider from "next-auth/providers/github";
import prisma from "../../../lib/prisma";

export default NextAuth({
export const authOptions = {
providers: [
GithubProvider({
clientId: process.env.GITHUB_ID,
Expand Down Expand Up @@ -30,4 +30,6 @@ export default NextAuth({
return token;
},
},
});
};

export default NextAuth(authOptions);
5 changes: 3 additions & 2 deletions pages/api/tasks/[taskId].js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { authOptions } from "../auth/[...nextauth]";
import prisma from "../../../lib/prisma";
import { getSession } from "next-auth/react";
import { getServerSession } from "next-auth/next";

export default async function handle(req, res) {
const session = await getSession({ req });
const session = await getServerSession(req, res, authOptions);

if (session) {
if (req.method === "GET") {
Expand Down
5 changes: 3 additions & 2 deletions pages/api/tasks/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { authOptions } from "../auth/[...nextauth]";
import prisma from "../../../lib/prisma";
import { getSession } from "next-auth/react";
import { getServerSession } from "next-auth/next";

const secret = process.env.NEXTAUTH_SECRET;

export default async function handle(req, res) {
const session = await getSession({ req });
const session = await getServerSession(req, res, authOptions);

if (session) {
if (req.method === "GET") {
Expand Down

0 comments on commit cfb6efe

Please sign in to comment.