Khan/aphrodite

StyleSheetServer will be null when imported in Jest

keroxp opened this issue · 4 comments

I'm using aphrodite for SSR and it works fine. But I got error while testing in Jest.

import {StyleSheetServer} from "aphrodite"
// StyleSheetServer will be null in Jest environment

Jest exports fake window object into global context before all modules are resolved. So we cannot determine if StyleSheetServer is imported from browser or server only by checking typeof window.

ref: https://github.com/Khan/aphrodite/blob/master/src/exports.js#L74

Do you have any idea to fix the issue? If not, please consider to change determination of browser / server environment?

Thanks.

You could try setting the testEnvironment: "node" in your jest settings. This will run the tests without JSDOM and the window object shouldn't be defined.

@kevinbarabash It works. Thank you 🙏

This isn't really a viable solution. For example we use a jsdom test environment as we have code that tests things such as local storage. I'd imagine this is the case for a lot of other users too.

Is there another solution? Seems like this one line fix is blocking us from using aphrodite in snapshots to it's full potential.

agree with @rickihastings. This should be exported regardless in my opinion.