-
Notifications
You must be signed in to change notification settings - Fork 28
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
feature: require pages in browser and non-browser environment #73
Conversation
|
||
export function jestIsolateModules<T>(f: () => T) { | ||
return new Promise((resolve, reject) => { | ||
jest.isolateModules(() => { |
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.
We will probably have to check if jest is defined
and have an approach in place that could work across different testing frameworks? Perhaps others do support manual modification of require.cache
and it wont be that complicated anyway.
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.
Yes, 100% agree. I'd love to keep this more or less framework agnostic.
This means providing a isolateModules
equivalent that works with vanilla require
. By now I found a few packages to bypass require's cache, but they don't consider nested requires:
We might search better or try our own implementation (if feasible). Still we wouldn't be able to support out of the box other test frameworks which provide their own require
reimplementation.
In order for the strategy taken in this PR to succeed, we need to wait until jest
publishes its next release with fixed isolatedModules
.
In the meanwhile I might try to patch Jest with patch-package
.
4febb83
to
56a4fc1
Compare
baf3e9d
to
21f2e23
Compare
Hey @Meemaw! I surprisingly got to get some outcome from this PR. There are quite a few open points to be more carefully considered and I'm sure you've a lot to bring into the discussion. What this PR doesThis PR enhances the way page files are required runtime. Each page is required twice:
The 2 page files are available through type PageObject = {
- page: NextPageFile;
+ page: {
+ client: NextPageFile;
+ server: NextPageFile;
+ }
route: string;
//...
}; The same happens for custom App and Document, required in both jest.isolateModules vs. jest.resetModulesIn order to retrieve both file versions,
Therefore, this PR means tying
Waiting for Jest'otThis PR also depends on JEST publishing this commit. Notes
|
This looks very promising @toomuchdesign 🚀 A few comments:
|
As for transforming |
I found out that bypassing Currently we're manually checking for There's one dilemma left: how do we test it?? Shall we use another test runner like |
Yes, if possible, it would be ideal to run the whole test suite with another test runner, e.g. |
c143cab
to
642730c
Compare
I though we might consider publishing this PR even before Jest gets published. On our side we've everything in place to support upcoming Jest version and (optimistically) any other test runner. On top of this, the issue addressed by this PR should affect a small part of users. Users might be still able to get the features provided by this PR by patching Jest, the same way we do. I added a note in the readme about it. As a downside, this would mean our users should remember to remove the patch when they'll update to Jest v27. How do you see it @Meemaw? Feasible or too risky? PS. happy holidays :) |
Hey 🎄 I think we should merge this PR as it unlocks some nice things ( |
Perfect! Let's merge, then. Take your time to stress this lib as much as you like. 🙌 |
What kind of change does this PR introduce?
This PR tries to extend current
server
idea to workaround #67 and #64. The main issue here consists of being able to access the same module in both "browser" and "non-browser" environment. This approach tries to consider "browser" mode modules as default and keep a "non-browser" copy of the relevant modules in aserver
instance initialized whengetPage
gets called.Relevant modules are:
next/dist/next-server/lib/side-effect
)What is the current behaviour?
You can also link to an open issue here.
What is the new behaviour?
Page components (including
_app
and_document
) are imported twice: with and without browser's global objects. There available throughpageObject.page.client
andpageObject.page.server
.Client versions are imported with global require cache while server versions gets imported via an isolated require cache (using
jest.isolatedModules
orstealthy-reqruire
).Does this PR introduce a breaking change?
Yes, but the result should be closer to what is expected from an actual Next.js instance running on server + client.
Users might optionally and temporarily patching jest until Jest v27 is released (see docs).
Other information:
Please check if the PR fulfills these requirements: