cypress-io/cypress-react-unit-test

How one can attach to window custom fields?

Relaxe111 opened this issue · 6 comments

Hello, I have setup a create-react-app typescript with cypress and try to access in my tested component the object window._env.MY_ENV_VAR and i got an error that _env is undefined and this is logical, but i can't figure out how to attach to window object custom variables then i run unit tests?

I wil try do do it today to show what i want

I've created a demo app with similar configuration which i have in production.
i have a component MyCompoentToTest.tsx and in useEffect i do a axios call to an url which is taken from window._env.:
image

I declare window._env = {...} in public/env.js
image

and import it in index.html file like :
image
to start cypress i run: yarn cypress:open
When i run integration test cypress/integration/App.spec.tsx i can access window._env and it works as i expect.

When i run MyComponentToTest.spec.tsx it throws an unhandled expception that window._env is undefined, which is logical, because in component test it mounts without public/index.html file where is declared window._env variable.
image

Now I Wonder how i can specify that in component test should exist window._env:{...} object which sould be taken from public/env.js?

my project example is here(I hope i understood correctly that i need to make an example project):
cypres-unit-test.zip

Perfect, now I understand what you mean!

Thank-you.

🎉 This issue has been resolved in version 4.16.0 🎉

The release is available on:

Your semantic-release bot 📦🚀