Skip to content

use partial mocking by default

Compare
Choose a tag to compare
@iambumblehead iambumblehead released this 06 Sep 15:55
302ca82

This 2.0.0 release includes changes incompatible with previous releases. A migration guide is included just below this small changelog list,


migration guide v1.9.8 => v2.0.0

  • change esmock.px(...args) to esmock(...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' to import { strict as esmock } from 'esmock', or
    • change esmock(...args) to esmock.strict(...args)

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.