chromaui/chromatic-cli

`isChromatic` fails in non-browser environment

Closed this issue · 2 comments

Bug report

I have some code that runs in both snapshots as well as in vitest (node), and I need to set a parameter differently between the two, so I tried to use isChromatic(). However, that fails in vitest with:

Cannot read properties of undefined (reading 'match')

From this line:

windowToCheck.location.href.match(/chromatic=true/))

Perhaps a guard could be added before checking match there?

I've gotten a similar failure / issue when using isChromatic in a react-native application (it's an RN + RN-web app which we run Chromatic on the web variant of).

In this case though, the failure didn't come from location.href being undefined, but from navigator.userAgent (line above line linked in this report) being undefined in a RN context.

Happy to propose a fix for both if you're accepting contribs :)

@IanVS @vicrep
Yes, isChromatic() doesn't work in the browser. Instead, you can use an environment variable as explained here.