Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(test): Switch rwjs/auth over to vitest #10423

Merged
merged 5 commits into from
Apr 5, 2024

Conversation

Tobbe
Copy link
Member

@Tobbe Tobbe commented Apr 5, 2024

Switches @redwoodjs/auth over from jest to vitest.

The one weird thing is I had to make RWJS_API_GRAPHQL_URL a full URL, or mock-service-worker would throw an error about invalid URL. require() or import of whatwg-fetch also doesn't seem to make a difference, so I removed it. @jtoar and I spent some time looking into it yesterday, but couldn't get a full understanding exactly what was going on. It's something about what Request is being used.

MDN notes (emphasis mine)

A string containing the URL of the resource you want to fetch. The URL may be relative to the base URL, which is the document's baseURI in a window context, or WorkerGlobalScope.location in a worker context.

So it sounds like what we had should have been allowed. And it was with jest. But now, with vitest, for some reason it isn't anymore. My hunch is that require('whatwg-fetch') or import 'whatwg-fetch' isn't polyfilling Request anymore (for some reason), so msw is using some other version of Request that doesn't allow relative URLs. I haven't (yet) been able to verify that though.

There's a StackOverflow question here that sounds like it might be related. But difficult to know for sure. https://stackoverflow.com/questions/76046546/fetch-error-typeerror-err-invalid-url-invalid-url-for-requests-made-in-test
The answer there is to update to the latest version of msw

Found this in the msw docs

If you are describing a network behavior for a Node.js application, use the absolute URLs you are requesting [...] polyfill the document.baseURI string
https://mswjs.io/docs/basics/intercepting-requests#how-do-i-use-relative-urls-in-nodejs

The thing is though, that document.baseURI seems to already be polyfilled. So still not sure what's going on.
image

On main
image

With vitest
image

So definitely a different one. Not sure why though.

I can "fix" it by doing this
image
image

But I would still very much like to understand what's actually going on here.

UPDATE:

  • whatwg-fetch gets loaded for both jest and vitest
  • jest creates a vmContext that it loads its modules in
  • vmContext is globalThis for modules. Basically what "window" is in a browser, but window.fetch (really globalThis.fetch) is undefined
  • When whatwg-fetch is loaded and sees that there is no globalThis.fetch it polyfills fetch by attaching its own implementation of fetch to globalThis, and its own implementation of Request etc
  • vitest loads modules using a normal node import statement.
  • In node (>= v18) there is a globalThis.fetch
  • When whatwg-fetch is loaded it sees that there already is a globalThis.fetch, so it doesn't try to polyfill anything

So that's the reason why there are two different versions of Request. And this is the reason why we need to manually set globalThis.Request = RequestPolyfill for vitest.

Furthermore, you can easily reproduce the "invalid url" error for relative urls in the Node version of Request by simply running node in your terminal and doing new Request('/relative'). As MDN states, relative urls are relative to the document's baseURI, but there is no document in node, so that's why you need full urls.

vitest does provide a value for document.baseURI, as we saw earlier (http://localhost:3000). But my guess here is that node's Request implementation doesn't even bother checking document, because it knows there will never be a document in the node environment. However, if you use a polyfill, like whatwg-fetch, they do look for document.baseURI and so it all works.

To keep the test (-environment) as close as possible to the jest environment we had I'm going to explicitly use the whatwg-fetch implementations of all its exports (rather than switching to a full url).

@Tobbe Tobbe added the release:chore This PR is a chore (means nothing for users) label Apr 5, 2024
@Tobbe Tobbe added this to the chore milestone Apr 5, 2024
@Tobbe Tobbe added the changesets-ok Override the changesets check label Apr 5, 2024
@Tobbe Tobbe enabled auto-merge (squash) April 5, 2024 13:32
@Tobbe Tobbe disabled auto-merge April 5, 2024 13:32
@Tobbe Tobbe merged commit a44da75 into redwoodjs:main Apr 5, 2024
46 checks passed
@Tobbe Tobbe deleted the tobbe-rw-auth-vitetst branch April 5, 2024 14:07
dac09 added a commit to dac09/redwood that referenced this pull request Apr 9, 2024
…auth-provider-p1

* 'main' of github.com:redwoodjs/redwood:
  fix(middleware): Handle POST requests in middleware router too (redwoodjs#10418)
  chore(ci): get ci running on next (redwoodjs#10432)
  RSC: Explain noExternal vite config option (redwoodjs#10429)
  chore(web): Fix .d.ts overwrite build issue (redwoodjs#10431)
  chore(web): .js imports to prep for ESM (redwoodjs#10430)
  chore(refactor): Split rwjs/forms up into several smaller logical units (redwoodjs#10428)
  chore(rsc): simplify `noExternals` config (redwoodjs#10220)
  chore(deps): Update vite to 5.2.8 (redwoodjs#10427)
  chore(auth): Convert `@redwoodjs/auth` to ESM+CJS dual build (redwoodjs#10417)
  chore(framework-tools): Warn about missing metafile (redwoodjs#10426)
  chore(test): Switch rwjs/auth over to vitest (redwoodjs#10423)
  chore(whatwg-fetch): Switch to importing instead of requiring (redwoodjs#10424)
  chore(deps): bump undici from 5.28.3 to 5.28.4 in /.github/actions/check_changesets (redwoodjs#10421)
  chore(route-manifest): Add relativeFilePath to route manifest (redwoodjs#10416)
dac09 added a commit that referenced this pull request Apr 11, 2024
…th-mw-auth

* 'main' of github.com:redwoodjs/redwood: (21 commits)
  fix(auth): Handle when authorization header is lowercased (#10442)
  Update rbac.md - code match (#10405)
  chore: make crwa e2e test work across branches (#10437)
  feat: [Auth] Common AuthProvider & use* changes for middleware auth (#10420)
  fix(cli): only show webpack options for dev if `bundler = "webpack"` (#10359)
  fix(vercel): specify build env vars as a string (#10436)
  fix(vercel): write `vercel.json` as a part of setup (#10355)
  fix(middleware): Handle POST requests in middleware router too (#10418)
  chore(ci): get ci running on next (#10432)
  RSC: Explain noExternal vite config option (#10429)
  chore(web): Fix .d.ts overwrite build issue (#10431)
  chore(web): .js imports to prep for ESM (#10430)
  chore(refactor): Split rwjs/forms up into several smaller logical units (#10428)
  chore(rsc): simplify `noExternals` config (#10220)
  chore(deps): Update vite to 5.2.8 (#10427)
  chore(auth): Convert `@redwoodjs/auth` to ESM+CJS dual build (#10417)
  chore(framework-tools): Warn about missing metafile (#10426)
  chore(test): Switch rwjs/auth over to vitest (#10423)
  chore(whatwg-fetch): Switch to importing instead of requiring (#10424)
  chore(deps): bump undici from 5.28.3 to 5.28.4 in /.github/actions/check_changesets (#10421)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changesets-ok Override the changesets check release:chore This PR is a chore (means nothing for users)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant