-
-
Notifications
You must be signed in to change notification settings - Fork 909
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
fix: add browser
condition to package.json
#616
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for raising this!
I think the patch looks correct and non-breaking but I'm still trying to fully understand the consequences, in particular if this will change anything about how webpack and rollup will behave.
A good first sign that the webpack and rollup behaviors remain unchanged is that the browser tests still pass with your patch.
I believe those tests cover the path where webpack and rollup are instructed to use the esm-browser
build.
Where I'm not entirely sure is cases, where webpack/rollup would pick the pkg.exports.browser.require
path. I think those cases, the overrides in pkg.browser
must be taken into account by the bundlers in order to generate a browser-compatible build.
Have you tested this case?
Nope, not tested. However, I don't think those fields are related? At least with EDIT: Yep, at least webpack behaves in this way: https://webpack.js.org/guides/package-exports/#notes-about-ordering
Might have misunderstood your question, are you worried about webpack getting the |
I just ran a quick test: 13ceb79 When using I have not verified with rollup yet. But at least for webpack, the I think this should be covered in the browser tests. I can't promise when I can find the time to do this though. Edit: The test in 13ceb79 is not complete, I added |
Their docs are wrong, then? Seems weird 😅 But if it works like you want it to, all is well? |
Ah, seems like |
Is it even valid to have a "browser" field nested w/in the "exports" block like this? If so, where might we find some sort of description or spec for how it's interpreted? |
https://nodejs.org/api/packages.html#nested-conditions
|
As @SimenB mentioned the However what I don't really like about the current approach in this PR is that we're mixing two concepts: Through I think a cleaner solution would be to provide a consistent CommonJS build for the browser, e.g. in However in this case now with I'm sorry for the long write up, I guess the main message is that this is complicated… @SimenB do you have an example of where a syntax error is thrown (what you reference in the original PR description)? I would really like to figure out what happens under the hood when jest runs these tests. |
A browser build in CJS would be even better of course 🙂
Jest provides https://github.com/SimenB/jest-uuid Remove comment at top of |
@ctavan how do we unblock this? 🙂 This issue is actually stopping me from using the Jest alpha at work 😅 (although I guess I can just patch |
Sorry for the silence and sorry to hear this blocks you. I'm happy to help unblock this but I have to admit that I'm not sure what the right way forward is. I experimented with building a browser commonjs build, but that only reveals the actual problem… If you run jest on
So the browser commonjs build uses browser APIs ( I'm currently failing to understand what the expectations are when you run a test case with If it's not reasonably close, I think that resolving the Can you help clarify the expectations? Edit: So to reiterate: Your patch only "works" because it effectively makes jest execute the node build instead of the browser build. |
That seems perfectly fine to me? "If you want to use
As close as possible. If APIs are missing from the env, the module should complain. The fact I want to use CJS instead of ESM is separate from browser vs node. It's not up to this module to say "you cannot use Or |
Jest 28 has now been released - which means that Jest and UUID are no longer compatible. For anyone trying to fix this issue - @dbjorge did a good temp solution here: microsoft/accessibility-insights-web#5421 (comment) But would be great to have a proper solution to this. |
Looping in @dbjorge here since I think this thread is more suitable to discuss the jest/uuid specific issue than #616 (comment). First of all, thanks for the great summary @dbjorge! At the risk of repeating what @dbjorge already summarized perfectly well, I'd like to highlight again: Jest + UUID just "accidentally" worked in the past because Jest used to pick the Node.js build even in environments like jsdom that were supposed to mimic the browser. Apparently this has worked well for folks in practice, so I think a custom jest resolver like this one is actually a great mitigation for the time being as it just puts us back to where we were before Jest 28. If we were adding a CommonJS browser build for completeness of the support matrix of @SimenB two questions:
I can see how this affects many people, given both jest and uuid are so widespread, I'm just still failing to clearly identify the right solution for this situation. |
@ctavan Thanks for the ping! jestjs/jest#9430 is the meta-issue tracking ESM support in Jest. It is a very long and very soul-crushing read. For the purposes of our discussion here, I think it's probably safe to say that it won't happen soon enough for the folks that need Jest+uuid workarounds to unblock Jest 28 updates. I want to emphasize that, as a user of both libraries, I don't think anyone on the uuid or Jest sides are making unreasonable decisions here; I don't really feel convinced that a workaround really "belongs" in either library. Particularly:
I think there are probably a large number of users that will be impacted by this, but more importantly than that, I want to recognize that neither Jest nor uuid have any obligation to be cross-compatible with one another. I am a thankful user of the work you have both made available for free, and neither project's maintainers owe me (and certainly not my employer) a damn thing; if I have to maintain a few lines of |
Hey! 👋
There is support today, however it's experimental (docs). And given the complete lack of progress on the V8/Node side of things (nodejs/node#37648, AFAIK latest is the (seemingly) stalled https://chromium-review.googlesource.com/c/v8/v8/+/3172764) it will stay experimental (as in you need to run
I don't think we'll be adding workarounds for any libraries directly in Jest. The easiest solution would probably be for somebody to create a
I 100% agree with this statement, well put! |
Thanks for chiming in @SimenB!
@dbjorge what do you think about this approach? Would this be something you'd consider to test-drive on accessibility-insights-web?
Or maybe rather this second approach? Given I currently don't have my own use case for this, I don't think I'll be able to provide a solution (like the |
For anyone coming here looking for a quick workaround, i did the following in my jest config (which i think is more or less @SimenB's suggestion with a moduleNameMapper: {
"^uuid$": "<rootDir>/node_modules/uuid/dist/index.js",
}, |
Yep! If your config is in JS, I'd recommend |
I actually tried basically this solution before I went with the custom resolver - I don't think a wrapper package is necessary, you can just do something like: moduleNameMapper: {
// This forces Jest/jest-environment-jsdom to use a Node+CommonJS version of uuid, not a Browser+ESM one
// See https://github.com/uuidjs/uuid/pull/616
//
// WARNING: if your dependency tree has multiple paths leading to uuid, this will force all of them to resolve to
// whichever one happens to be hoisted to your root node_modules folder. This makes it much more dangerous
// to consume future uuid upgrades. Consider using a custom resolver instead of moduleNameMapper.
'^uuid$': require.resolve('uuid'),
} However, I rejected this in favor of the custom resolver because my understanding of the edit: beaten twice, oops |
@dbjorge your analysis is correct though, this forces a single version across the entire tree, so might not be correct for all use cases. a |
JSDOM v20 supports |
@SimenB I'm being lazy here (headed out the door to walk dog, get my day started), but does that mean things should "just work" now? ... or that work is still needed w/in this module to resolve this issue? |
It means the browser version can be ported to CJS (so just module format change, no logic change needed (iirc)) |
I can confirm that this now works with |
Jest runs browser tests in a jsdom environment which now also supports web crypto polyfills. Since ESM support in Jest is currently still limited, it requires a commonJS build for browser environments, see the discussion in #616 for all the details. Co-authored-by: Christoph Tavan <dev@tavan.de>
Jest runs browser tests in a jsdom environment which now also supports web crypto polyfills. Since ESM support in Jest is currently still limited, it requires a commonJS build for browser environments, see the discussion in #616 for all the details. Co-authored-by: Christoph Tavan <dev@tavan.de>
Jest runs browser tests in a jsdom environment which now also supports web crypto polyfills. Since ESM support in Jest is currently still limited, it requires a commonJS build for browser environments, see the discussion in #616 for all the details. Co-authored-by: Christoph Tavan <dev@tavan.de>
Jest runs browser tests in a jsdom environment which now also supports web crypto polyfills. Since ESM support in Jest is currently still limited, it requires a commonJS build for browser environments, see the discussion in #616 for all the details. Co-authored-by: Christoph Tavan <dev@tavan.de>
Jest runs browser tests in a jsdom environment which now also supports web crypto polyfills. Since ESM support in Jest is currently still limited, it requires a commonJS build for browser environments, see the discussion in #616 for all the details. Co-authored-by: Christoph Tavan <dev@tavan.de>
Jest runs browser tests in a jsdom environment which now also supports web crypto polyfills. Since ESM support in Jest is currently still limited, it requires a commonJS build for browser environments, see the discussion in #616 for all the details. Co-authored-by: Christoph Tavan <dev@tavan.de>
Jest runs browser tests in a jsdom environment which now also supports web crypto polyfills. Since ESM support in Jest is currently still limited, it requires a commonJS build for browser environments, see the discussion in #616 for all the details. Co-authored-by: Christoph Tavan <dev@tavan.de>
Jest runs browser tests in a jsdom environment which now also supports web crypto polyfills. Since ESM support in Jest is currently still limited, it requires a commonJS build for browser environments, see the discussion in #616 for all the details. Co-authored-by: Christoph Tavan <dev@tavan.de> Co-authored-by: Simen Bekkhus <sbekkhus91@gmail.com>
I have released |
I just gave it a try and it seems to fix the issue on my side. Looking forward for the stable release(s). Thanks for your work 💪 |
@ctavan can confirm it allows us to remove the |
FWIW, Jest 29 is released (which means |
I have no idea, Jest 29 has been out for a week |
Thanks for the info. Awaiting uuid v9 now then :) |
|
jest@28
will ship withexports
support. But when usingjest-environment-jsdom
to simulate a browser environment, we don't provide thenode
condition, meaning it resolves todefault
. Unless ESM is enabled, this results in a syntax error.