Cannot find module '@auth/drizzle-adapter' #983
-
Thanks for building this library! I'm wondering if there's any setup for Jest & Next.js required that's not documented because the readme jumps straight into installing I've followed the guide at https://nextjs.org/docs/app/building-your-application/testing/jest but using import type { Config } from "jest";
import nextJest from "next/jest.js";
const createJestConfig = nextJest({
// Provide the path to your Next.js app to load next.config.js and .env files in your test environment
dir: "./",
});
// Add any custom config to be passed to Jest
const config: Config = {
coverageProvider: "v8",
testEnvironment: "node",
// Add more setup options before each test is run
// setupFilesAfterEnv: ['<rootDir>/jest.setup.ts'],
};
// createJestConfig is exported this way to ensure that next/jest can load the Next.js config which is async
export default createJestConfig(config); I have a simple test for a import { testApiHandler } from "next-test-api-route-handler";
import * as appHandler from "./route";
it("returns 403 unauthorized when not authenticated", async () => {
expect.hasAssertions();
await testApiHandler({
appHandler,
test: async ({ fetch }) => {
await expect((await fetch()).json()).resolves.toStrictEqual({
error: "Unauthorized",
});
},
});
}); But when I run
Have I missed something? |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 8 replies
-
I was able to solve this by mocking the // route.test.ts
import { testApiHandler } from "next-test-api-route-handler";
import * as appHandler from "./route";
import type { Session } from "next-auth";
let mockedSession: Session | null = null;
// Mock authOptions
jest.mock("@/lib/auth", () => ({
authOptions: {
adapter: {},
providers: [],
callbacks: {},
},
}));
jest.mock("next-auth/next", () => ({
getServerSession: jest.fn(() => Promise.resolve(mockedSession)),
}));
afterEach(() => {
mockedSession = null;
});
it("returns 403 unauthorized when not authenticated", async () => {
mockedSession = null;
await testApiHandler({
appHandler,
test: async ({ fetch }) => {
const response = await fetch({ method: "GET" });
console.log(await response.json());
expect(response.status).toBe(401);
},
});
}); And this test now passes:
However, if I attempt to check the returned JSON: it("returns 403 unauthorized when not authenticated", async () => {
mockedSession = null;
await testApiHandler({
appHandler,
test: async ({ fetch }) => {
const response = await fetch({ method: "GET" });
console.log(await response.json());
expect(response.status).toBe(401);
await expect(response.json()).resolves.toStrictEqual({
error: "Unauthorized",
});
},
});
}); I get an error:
I have |
Beta Was this translation helpful? Give feedback.
-
Mocking the use of it("returns 403 unauthorized when not authenticated", async () => {
mockedSession = null;
await testApiHandler({
appHandler,
test: async ({ fetch }) => {
const response = await fetch({ method: "GET" });
console.log(await response.json());
expect(response.status).toBe(401);
/*await expect(response.json()).resolves.toStrictEqual({
error: "Unauthorized",
});*/
},
});
}); This works correctly. However, if I uncomment
|
Beta Was this translation helpful? Give feedback.
-
Hi there! NTARH is conceptually quite simple: it's a thin wrapper around That last error is interesting though, |
Beta Was this translation helpful? Give feedback.
-
This has been addressed in v4.0.5. I'll be using your demo repo as an integration test for |
Beta Was this translation helpful? Give feedback.
This has been addressed in v4.0.5.
I'll be using your demo repo as an integration test for
next/jest.js
and SWC going forward :) If the latest version of NTARH still isn't working for you, please open a new issue and I'll investigate.