mswjs / msw

Industry standard API mocking for JavaScript.
https://mswjs.io
MIT License
15.97k stars 519 forks source link

feat: use original tough-cookie v5 #2356

Open SamMousa opened 1 week ago

SamMousa commented 1 week ago

This PR aims to drop the wrapper of tough-cookie: https://github.com/bundled-es-modules/tough-cookie It also upgrades the dep to v5.

The original PR: https://github.com/mswjs/msw/pull/2206 used the es-bundled version. In september with the release of v5 tough-cookie was rewritten in typescript and (I'm assuming here) is probably compatible with ES without this wrapper package.

kettanaito commented 5 days ago

Hi, @SamMousa. This looks exciting! The change requires a rewrite of our cookie store because the new tough-cookie exposes breaking changes in its methods (mostly in types). I will look into this once I have a minute.

SamMousa commented 5 days ago

I was hoping tests in ci would give me pointers, since running tests locally in the js / ts ecosystem is always a nightmare

kettanaito commented 5 days ago

@SamMousa, give this repo a try, the tests should be running without issues. Install dependencies, and you have three levels of tests to run:

The cookie store changes affect both Node.js and the browser, so start from running Node.js tests locally.

You can spot the mismatch in types if you open the module you modified.

SamMousa commented 3 days ago

I've simplified the implementation a lot and also reduced coupling with tough-cookie:

SamMousa commented 3 days ago

Pff, it took me a while to realize that I have to run node 18. Tests are really still brittle; running pnpm test:browser leads to enormous amounts of failed tests.

This PR is almost done.

I've simplified the code even further; no code depends on tough-cookie, except cookieStore.ts, cookieStore exposes only the minimum functionality MSW uses, this reduces coupling and if we want to we can more easily swap to other cookie libraries in the future.

I'm not familiar with pnpm and therefore not sure how to resolve the pnpm-lock.yaml conflicts. I just redid the changes in dependencies directly from main by running these commands:

> pnpm remove @bundled-es-modules/tough-cookie
> pnpm add tough-cookie

This is ready for review, please run the tests in CI to see what's wrong, locally, as expected, browser tests have high failure rates (on main as well).