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

Usage in close-to-real SSR environment? #605

Closed
xobotyi opened this issue Apr 21, 2021 · 4 comments · Fixed by #607 or #799
Closed

Usage in close-to-real SSR environment? #605

xobotyi opened this issue Apr 21, 2021 · 4 comments · Fixed by #607 or #799
Labels
bug Something isn't working good first issue Good for newcomers released

Comments

@xobotyi
Copy link
Contributor

xobotyi commented Apr 21, 2021

Is there any way to test SSR functionality in environment close to real SSR?

I'm testing the hook that should fallback to noop during SSR, the hook declaration itself, as for now i'm checking SSR by testing window type: typeof window !== 'undefined'. Like this:

export const myAwesomeHook = typeof window === 'undefined' ? ()=>{} : ()=>{/*some real hook code*/}

When testing with jest in it's common env (jsdom) window object is obviously exists and there is no way to test that everything will be fine in SSR, since i gaij browser version of a hook.
If to switch environment to node, ssr tests failing with

    The error below may be caused by using the wrong test environment, see https://jestjs.io/docs/en/configuration#testenvironment-string.
    Consider using the "jsdom" test environment.

    ReferenceError: document is not defined

       7 |
    >  8 |     renderHook(() => myAwesomeHook());
         |     ^
       9 |

In this case renderHook improted from server: import { renderHook } from '@testing-library/react-hooks/server';

@xobotyi xobotyi added the question Further information is requested label Apr 21, 2021
@mpeyper
Copy link
Member

mpeyper commented Apr 22, 2021

Hi @xobotyi, thanks for raising this issue. I think it's being caused by this line:

const container = document.createElement('div')

We should move that into the hydrate function so we reference document when they want it to render for real.

Are you interested in submitting a PR for this @xobotyi?

@mpeyper mpeyper added bug Something isn't working good first issue Good for newcomers and removed question Further information is requested labels Apr 22, 2021
@xobotyi
Copy link
Contributor Author

xobotyi commented Apr 22, 2021

@mpeyper i'm not really into SSR render flow, but if solution is only about that -i'll do.

mpeyper pushed a commit that referenced this issue Apr 22, 2021
* fix(ssr): div container created only during hydration

It allows to run tests in real node environment, where no DOM global
objects exists.

Fix: #605

* chore: add xobotyi to hall of fame %)
@github-actions
Copy link

🎉 This issue has been resolved in version 5.1.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

jsjoeio pushed a commit to jsjoeio/react-hooks-testing-library that referenced this issue Feb 16, 2022
jsjoeio pushed a commit to jsjoeio/react-hooks-testing-library that referenced this issue Feb 20, 2022
jsjoeio pushed a commit to jsjoeio/react-hooks-testing-library that referenced this issue Feb 26, 2022
jsjoeio added a commit to jsjoeio/react-hooks-testing-library that referenced this issue Feb 27, 2022
koobawuah added a commit to koobawuah/react-hooks-testing-library that referenced this issue Jun 16, 2022
koobawuah added a commit to koobawuah/react-hooks-testing-library that referenced this issue Jun 16, 2022
koobawuah added a commit to koobawuah/react-hooks-testing-library that referenced this issue Jun 16, 2022
@github-actions
Copy link

🎉 This issue has been resolved in version 8.0.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

lavaldi added a commit to lavaldi/react-hooks-testing-library that referenced this issue Jun 18, 2022
NgoranA added a commit to NgoranA/react-hooks-testing-library that referenced this issue Jun 19, 2022
josegarrera added a commit to josegarrera/react-hooks-testing-library that referenced this issue Jun 20, 2022
josegarrera added a commit to josegarrera/react-hooks-testing-library that referenced this issue Jun 21, 2022
d3mola added a commit to d3mola/react-hooks-testing-library that referenced this issue Jun 28, 2022
SK02K1 added a commit to SK02K1/react-hooks-testing-library that referenced this issue Aug 10, 2022
jonaskinnvall added a commit to jonaskinnvall/react-hooks-testing-library that referenced this issue Oct 31, 2022
jonaskinnvall added a commit to jonaskinnvall/react-hooks-testing-library that referenced this issue Oct 31, 2022
jonaskinnvall added a commit to jonaskinnvall/react-hooks-testing-library that referenced this issue Nov 6, 2022
jonaskinnvall added a commit to jonaskinnvall/react-hooks-testing-library that referenced this issue Nov 6, 2022
novicedev7291 added a commit to novicedev7291/react-hooks-testing-library that referenced this issue Nov 12, 2022
stephenkilbourn added a commit to stephenkilbourn/react-hooks-testing-library that referenced this issue Nov 15, 2022
stephenkilbourn added a commit to stephenkilbourn/react-hooks-testing-library that referenced this issue Nov 15, 2022
stephenkilbourn added a commit to stephenkilbourn/react-hooks-testing-library that referenced this issue Nov 15, 2022
jfet97 added a commit to jfet97/react-hooks-testing-library that referenced this issue Dec 26, 2022
jfet97 added a commit to jfet97/react-hooks-testing-library that referenced this issue Dec 26, 2022
jfet97 added a commit to jfet97/react-hooks-testing-library that referenced this issue Dec 26, 2022
jfet97 added a commit to jfet97/react-hooks-testing-library that referenced this issue Dec 26, 2022
ryanyogan added a commit to ryanyogan/react-hooks-testing-library that referenced this issue Jan 16, 2023
fprl added a commit to fprl/react-hooks-testing-library that referenced this issue Feb 16, 2023
fprl added a commit to fprl/react-hooks-testing-library that referenced this issue Feb 16, 2023
fprl added a commit to fprl/react-hooks-testing-library that referenced this issue Feb 19, 2023
fprl added a commit to fprl/react-hooks-testing-library that referenced this issue Feb 19, 2023
electrostaticfleece added a commit to electrostaticfleece/react-hooks-testing-library that referenced this issue May 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers released
Projects
None yet
2 participants