timkindberg / jest-when

Jest support for mock argument-matched return values.
MIT License
734 stars 38 forks source link

Resolves #6 - customize default return value #8

Closed jeyj0 closed 5 years ago

jeyj0 commented 5 years ago

I went for the proposed syntax:

const mock = jest.fn();
when(mock)
  .mockReturnValue('default')
  .calledWith('blah')
  .mockReturnValue('test');

It was easier to implement and looks more like jest, in hindsight.

One notable thing: when using the default without a .calledWith(..), an error is thrown once the mocked function is called, as it 1. won't work, and 2. is unnecessary - one could simply use jest's default mocking in this case. This was a subjective change, and could be taken out. If one would replace the implementation with the throw, with one that returns the default value, that should make it work, but I'd encourage users to use jest's default mocking for that.

If however this behavior is liked, it'd be super easy and reduce complexity to throw the error when only using when(..) without anything else as well. :smile:

codecov-io commented 5 years ago

Codecov Report

Merging #8 into master will not change coverage. The diff coverage is 100%.

Impacted file tree graph

@@          Coverage Diff          @@
##           master     #8   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files           2      2           
  Lines          53     60    +7     
=====================================
+ Hits           53     60    +7
Impacted Files Coverage Δ
src/when.js 100% <100%> (ø) :arrow_up:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update cac87b8...1ced75a. Read the comment docs.

timkindberg commented 5 years ago

Released as v2.1.0. Thanks!!!!!!!