Jest: Automatically calling update() from before matching a snapshot?

Created on 27 Sep 2017  路  11Comments  路  Source: facebook/jest

Would it be possible to roll a lib that automatically called enzymes update() before a snapshot? from what i see the assertion part that controls that is not extendable but would resolve a lot of the issues introduced with v3 regarding needing update() when dealing with instance().somemethodthatupdatesstate()

the root question i guess is there a way to extend the assertion library to add custom methods?

All 11 comments

Hey! Thanks for the question, unfortunately I don't know enough about whether that makes sense or not for enzyme. Do you mind looping in some enzyme maintainers to discuss this?

Absolutely! summoning @ljharb if anyone would know it would be him.

My thoughts behind this is a snapshot assumes to have the most up to date version of the dom for comparison reasons so calling update beforehand makes sense but i could be way off base.

Does jest have any enzyme-specific handling already? I'd assume not.

Personally, I find snapshot testing horrifically brittle in both technical and human terms, and I discourage anyone from using it.

That said, I would not expect update to automatically be called; if you want it updated, you should update it before you assert on it. I'm sure you could write a lib that does this for you, but if it made sense for snapshots, it'd also make sense for enzyme itself - so the fact that enzyme does not do it automatically means that it doesn't actually make sense for snapshots.

First: Thanks for the quick response!

I and many others find snapshots amazingly useful but to each their own.

But to your point that was my initial question was if it was possible for me to roll a lib that did it for me but i didn't see a way to extend upon jests expect methods how i'd need

For that I'll have to defer to @cpojer :-) but I don't think auto-updating wrappers is a good idea unless it's something enzyme does internally.

@ljharb the reason for this question is due to the changes with enzyme 3 that cause calls to wrappedcomponent.instance().someAction() to not get the latest dom which is quite problematic to existing tests. this seemed like an easy win.

I totally understand that it'd be easier than to change tests; but I think it'd be more correct (and thus better for long-term maintenance) to add an explicit update call to each of those tests.

oh i totally agree but with 1000 tests it's going to take a minute, thus why i wanted the ability to roll a lib that auto did it so we could update incrementally

@th3fallen Enzyme has a snapshot serializer accepting Enzyme object instance, so technically there's a possibility to call whatever method possible before serialization. Ping @rogeliog or just send him a friendly PR with the feature :) Here's the repo: https://github.com/rogeliog/jest-serializer-enzyme

(Closing as it's unlikely that Jest will need to change for this. Please continue the conversation!)

thanks @thymikee will do! and thanks again @cpojer and @ljharb for your insight you guys are awsome!

Was this page helpful?
0 / 5 - 0 ratings