paralleldrive / cuid

Collision-resistant ids optimized for horizontal scaling and performance.
Other
3.42k stars 123 forks source link

Is there any easy way to mock this lib with jest? #274

Closed ahctangU closed 1 year ago

ahctangU commented 1 year ago

It seems like due to the way the modules are exported the usual ways for exporting via jest don't work? Or is there something really obvious that I haven't found?

ericelliott commented 1 year ago

I don't mock it. Instead, for functions that generate ids, I take the id as an optional parameter. When the id is not passed in, call cuid to generate it. In unit tests, I just pass the id in.

Alternatively, you can copy the id from the actual results before running your expectations. I have never needed to mock cuid.