Testing with Mocha/Chai #2353
Unanswered
leo-petrucci
asked this question in
Help
Replies: 2 comments 3 replies
-
You shouldn't need to manually test the sign-in flow, that is our job. :) I would rather try to mock the endpoints. cc @lluia could we add this to the documentation about testing as it has come up before? |
Beta Was this translation helpful? Give feedback.
3 replies
-
I figured it out. I suck at non-frontend testing so there's probably better ways. I'm using Jest (as it's the only test runner I know how to mock external packages with) and chai (as I already have a setup for testing my serverless api). import chai from "chai";
import chaiHttp from "chai-http";
import { getSession } from "next-auth/client";
// This is the root of my graphql api that uses Apollo Server Micro
import server from "../../pages/api";
let app: any;
chai.use(chaiHttp);
// mocking the getSession function so we can pretend we're logged in
jest.mock("next-auth/client", () => ({
getSession: () => ({
user: {
name: "User",
email: "[email protected]",
image: null,
role: "USER",
},
accessToken:
"xxx",
expires: "2021-08-11T20:46:37.984Z",
}),
}));
beforeAll(async () => {
app = await server;
});
it("gets protected query", async () => {
const res = await chai
.request(app)
.post("/api")
.send({ query: "{ status { online } }" });
expect(res.body.status.online).toBe(true);
}); |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Question 💬
I'm currently trying to test my endpoints with mocha/chai but having some trouble understanding how. I found this thread with an explanation from some time ago but I haven't been able to get it to work.
As far as I understand it
auth/csrf
returns the cookie I need to authenticate, but I'm still unclear how I can log-in within my code?The person in the thread seems to be doing:
But doesn't give any explanation on what the code is doing or how to replicate it in other environments.
Is there an official example anywhere on how to get
next-auth
to work with mocha/chai?How to reproduce ☕️
My code is pretty much the same as the one in the other thread, but obviously missing the stuff I couldn't understand. Not sure if this helps.
Contributing 🙌🏽
No, I am afraid I cannot help regarding this
Beta Was this translation helpful? Give feedback.
All reactions