Skip to content

v3.4.0

Compare
Choose a tag to compare
@timkindberg timkindberg released this 20 Sep 00:07
· 24 commits to master since this release

New Feature: Use mockReturnValue after or without calledWith usage

Works with mockReturnValue, mockResolvedValue, mockRejectedValue, mockImplementation

Previously you were not allowed to set a default implementation with a mock return function without a calledWith:

when(fn).mockReturnValue(true)
fn() // ⚠️ Threw error!

Now you can!

when(fn).mockReturnValue(true)
fn() // ✅ Returns `true`

Also you could set the default after using calledWith:

when(fn).mockReturnValue('b') // ✅ Before was supported
when(fn).calledWith(1).mockReturnValue('a')
when(fn).mockReturnValue('b') // ⚠️ After was not

Now you can!

when(fn).mockReturnValue('b') // ✅ Before is still supported
when(fn).calledWith(1).mockReturnValue('a')
when(fn).mockReturnValue('b') // ✅ After is now supported

New Feature: Keep original function implementation when not matched

class TheClass {
  fn () {
    return 'real'
  }
}
const instance = new TheClass()

const spy = jest.spyOn(instance, 'fn')
when(spy)
  .calledWith(1)
  .mockReturnValue('mock')
expect(instance.fn(2)).toBe('real') // ✅ Successfully falls back to the real underlying spied function

Installation Improvement: Jest is now a peer dependency

This helps avoid installing lots of duplicated dependencies.

We presume that you were not implicitly relying on jest-when to install the jest dependency in your codebase. And assuming this we are able to bump by a minor instead of major version.

Tested with jest 24, 25, 26 and 27, it should work with other versions as well. We will likely not work to support more than the last couple versions without community PRs.

Commits:

  • Allow defaults to be set up after calledWiths or even without any calledWiths cc6ca58
  • Merge pull request #82 from mfressdorf/master fe6dba9
  • Merge pull request #79 from timkindberg/dependabot/npm_and_yarn/path-parse-1.0.7 bbb5396
  • Merge pull request #74 from timkindberg/dependabot/npm_and_yarn/hosted-git-info-2.8.9 6a523f9
  • Merge pull request #84 from chyzwar/mmake-jest-peer-deps 26a907a
  • Merge branch 'master' into mmake-jest-peer-deps 1379b18
  • Merge pull request #85 from AndrewSouthpaw/replace-bunyan-with-smaller-logger d591350
  • Fully remove logger. fb72636
  • Replace bunyan with more stable, zero-dependency logger. 5624306
  • fix(): lower peer deps to jest 24, make it non breaking ad2084a
  • feat(): make jest an peer dependancy 4092a59
  • Keep original function implementation when not matched 9b81e2b
  • Bump path-parse from 1.0.5 to 1.0.7 1eca1bc
  • Bump hosted-git-info from 2.6.0 to 2.8.9 40e8ef1
  • Merge pull request #73 from timkindberg/dependabot/npm_and_yarn/glob-parent-5.1.2 8af0e4c
  • Merge pull request #72 from timkindberg/dependabot/npm_and_yarn/normalize-url-4.5.1 f8c822e
  • Merge pull request #71 from timkindberg/dependabot/npm_and_yarn/trim-newlines-3.0.1 5c3ef60
  • Merge pull request #70 from timkindberg/dependabot/npm_and_yarn/lodash-4.17.21 0a35f5b
  • Merge pull request #69 from timkindberg/dependabot/npm_and_yarn/handlebars-4.7.7 83e8d88
  • Bump glob-parent from 5.1.1 to 5.1.2 73da46c
  • Bump normalize-url from 4.5.0 to 4.5.1 2577978
  • Bump trim-newlines from 3.0.0 to 3.0.1 a3b5623
  • Bump lodash from 4.17.19 to 4.17.21 d397bc1
  • Bump handlebars from 4.5.3 to 4.7.7 a32694c

v3.3.1...v3.4.0