use partial mocking by default
This 2.0.0 release includes changes incompatible with previous releases. A migration guide is included just below this small changelog list,
- exports a "strict mocking" version of esmock
- uses "partial mocking" behaviour with default export and updates the readme,
- resolves error when partial mocking modules not found on filesystem
- renames option
isPackageNotFoundError
toisModuleNotFoundError
- see the release announcement for details and
migration guide v1.9.8 => v2.0.0
- change
esmock.px(...args)
toesmock(...args)
. "px" or "partial mocking" behaviour is the default behaviour in the new version - change
{ isPackageNotFoundError: false }
to{ isModuleNotFoundError: false }
this option is renamed to be more accurate - to use "partial mocking" automatically, continue using
esmock(...args)
- to avoid "partial mocking" and to continue using "strict mocking" (explained below), use one of the following changes,
- change
import esmock from 'esmock'
toimport { strict as esmock } from 'esmock'
, or - change
esmock(...args)
toesmock.strict(...args)
- change
The new version of esmock uses "partial mocking" by default which merges mock definitions with original module definitions. Previous versions used "strict mocking", where mock definitions are not modified or merged with original module definitions. Strict mocking is still available through esmock.strict
and import { strict } from 'esmock'
.
To demonstrate the difference, a target module and its usage with esmock,
import p from 'path'
console.log(p.dirname('/dog.png'), p.basename('./dog.png'))
import esmock, { strict } from 'esmock'
esmock('./logpath.js', { path: { basename: () => 'cat.png' } })
// "/ cat.png"
strict('./logpath.js', { path: { basename: () => 'cat.png' } })
// Error "The requested module 'path' does not provide an export named 'dirname'"
Examples of both default and strict mocking use "path" definitions that define "basename" only and not "dirname". The regular, "partial mocking" behaviour merges the mocked "path" definition with the core "path" definition, including "path.dirname". The "strict mocking" behaviour does not modify the mock definition and "dirname" is never defined, resulting in a runtime error.