After creating custom jest matchers, I decided to test them using snapshot testing. Surprisingly, the tests passed on my local Windows environment but failed in the CI on Linux. Strangely enough, the output for the failing tests was identical.
Determined to solve this issue, I attempted to recreate the problem by running the test in my WSL, where it actually passed. I then tried it on another Linux server, and once again, the tests passed without any issues.
I initially suspected a line ending problem, but upon checking, I confirmed that my line endings were set to LF. This led me to further investigate by:
- Printing the snapshot
- Updating the snapshot
- Printing it again
To my surprise, I discovered that my snapshots contained ANSI escape codes. After some research, I came across a helpful comment on a GitHub issue - Snapshot comparison failing but results look identical where the collaborator suggested:
You can try running with
NO_COLOR=1
orCI=true
I experimented by trying both options simultaneously on my local machine, but unfortunately, it did not work. Then, I tested it on WSL and encountered a failure. Removing the CI=true
(as I suspected it had something to do with the ANSI) made the tests pass. Reversely, enabling only CI=true
caused the tests to fail.
As I don't want to limit my tests to just WSL, I am determined to find an alternative solution to ensure consistent snapshots across different environments.