remix-run / example-trellix

A partial trello board clone with Remix
https://trellix.fly.dev
290 stars 47 forks source link

How would you test cookies? #7

Open EnriqueCepeda opened 7 months ago

EnriqueCepeda commented 7 months ago

Hi! In my company, we're using Remix, and we're making more or less the same approach of auth with Cookies, but haven't been able to test it correctly. We're trying to test that a loader has a cookie that an action has previously set with: redirect("/", {headers: {"Set-Cookie": "cookie=foo"}}). The problem is that the loader doesn't receive any cookie, and it has been impossible to test it using remixStub.

Testing auth is very important for us, but ideally, we don't want to start using an e2e tool such as playwright because Vitest (+ happy-dom) is way faster, is there anything you think we might be missing or do you have any comment?

Furthermore, it would be awesome if you show us how would you to test these kinds of examples!

Sarvesh-Ghildiyal commented 4 months ago

Hello @EnriqueCepeda, as per my understanding of the question, I think using our request object does give us access to the created cookie. you can do so like this.

export async function loader(request:LoaderFunctionArgs) {

 const cook= await request.request.headers.get("cookie")
  console.log(cook)
  return null;

}

Although this approach would not be greatly suggested, rather have a server side module or function where you can call in the cookie and check up on things in any way you would like to, and you might call that function in any of the loader you would prefer to.