-
@housecor A pattern I've picked up from ruby/rails/rspec tests goes one step further with those "after" tests -- anywhere you're tempted to write
....when X happens, that scenario instead becomes another nested describe block. -
@housecor In Rspec, you'd reach for the
contextfunction instead ofdescribefor those. It's literally the same thing asdescribe, except for the semantics. Acontextdescribes a scenario; adescribedescribes a thing. -
@housecor so in your example it'd look like describe('auth')... describe('ssr handler')... context('when ensureAuthenticatedThrows')... it('returns 403')...
-
@housecor Which I think reads nicely as a story about what's expected! And it also allows you to group tests within contexts, vs repeating those lengthy "...when x happens" names.
-
@housecor
contextexists in rspec but it doesn't exist in jest, so I've been usingdescribeto apply to both _things_ & _scenarios_, but I quite like this pattern. -
@housecor I wrote about this and the
letblock that I wish existed in javascript tests at stevenhicks.me/blog/2021/09/what-javascript-tests-could-learn-from-rspec/. As I wrote there, it's very possible I'm pushing this pattern too far in my tests but hey when is that not the case 😅😬