5 ms·
Can anyone explain to me why the `expect` type of assertion library caught on so much in the javascript land? I mean how is expect(2).toBeLessThanOrEqualT
by Hurtak 10y ago
Can anyone explain to me why the `expect` type of assertion library caught on so much in the javascript land?
I mean how is
expect(2).toBeLessThanOrEqualTo(3)
more readable than
assert(2 <= 3)
Especially when we have packages like power-assert (1)
[1] https://github.com/power-assert-js/power-assert https://github.com/power-assert-js/power-assert
- oakesm9 10y agoI think it comes from the Ruby community influence where the language and tools aim to read like natural language.
- SirensOfTitan 10y agoThis has a couple advantages: 1. A test that reads as natural language is easy to understand. Straightforward, declarative tests are a big win IMO. 2. This allows for better test result reporting: instead of assertion failed, the utility can tell you more (expected 4 but got 3).
- jlongster 10y ago> To use power-assert, you need to transform your test code for power-assert output. That's a pretty big requirement. I usually avoid the long names like that, but doing `expect(3).toEqual(4)` isn't too bad and it allows the test runner to provide really good error messages.
- hashkb 10y agoBecause you get way better error reporting. Asserting on a boolean is just pass/fail, but with expect the runner tells you "got X, expected Y" which is way better.
- danabramov 10y agoBecause: A) It doesn't require transforms to bring you good messages. B) <= is an "easy" case, but what about deep equality, recursive matchers[1], or snapshots[2]? [1]: http://facebook.github.io/jest/docs/expect.html#expectobjectcontainingobject http://facebook.github.io/jest/docs/expect.html#expectobject... [2]: http://facebook.github.io/jest/docs/snapshot-testing.html http://facebook.github.io/jest/docs/snapshot-testing.html