@tapjs/mock
tap plugin adding t.mockRequire() and t.mockImport()
Last updated a year ago by isaacs .
BlueOak-1.0.0 · Repository · Bugs · Original npm · Tarball · package.json
$ gnpm install @tapjs/mock 
SYNC missed versions from official npm registry.

@tapjs/mock

A default tap plugin adding t.mockRequire(), t.mockImport(), and t.createMock()

USAGE

This plugin is installed with tap by default. If you had previously removed it, you can tap plugin add @tapjs/mock to bring it back.

This is the way to do dependency injection at the module level. When the loaded module, or anything it loads, loads something that you've mocked, it'll get your mock instead of the real thing. Useful for getting into those hard to trigger code paths.

// test.mts
import t from 'tap'

t.test('handls stat failure by throwing', async t => {
  const mockStatSync = (p: string) => {
    t.equal(p, 'filename.txt')
    throw Object.assign(new Error('expected error'), {
      code: 'ENOENT',
    })
  }
  // do 'as typeof import(...)' so that TS knows what it returns
  const thingThatDoesStat = (await t.mockImport(
    '../dist/my-statty-thing.js',
    { 'node:fs': { statSync: mockStatSync } }
  )) as typeof import('../dist/my-statty-thing.js')

  t.throws(() => thingThatDoesStat('filename.txt'), {
    message: 'expected error',
    code: 'ENOENT',
  })
})

t.mockImport(module, [mocks]): Promise<any>

Load the module with import(). If any mocks are provided, then they'll override the module's imported deps. This works for both ESM and CommonJS modules.

t.mockRequire(module, [mocks]): any

Same as t.mockImport(), but synchronously using require() instead. This only works with CommonJS, and only mocks CommonJS modules loaded.

t.mockAll(mocks?: Record<string,any> | null): Record<string, any>

Convenience method to set the mocks for all subsequent calls to t.mockRequire or t.mockImport for the remainder of the test.

Mocks added with mockAll are overridden by any explicit mocks set in the t.mockRequire or t.mockImport call.

Repeated calls to t.mockAll() will add mocks to the set. If the same name is used again, it will replace the previous value, not merge.

If a key is set to undefined or null, then it will be removed from the mockAll set.

Reset by calling t.mockAll(null)

Call with no args to return the current mockAll object.

t.createMock(originalModule, mockOverrides): mockedModule

Sometimes you only want to override one function or property, perhaps buried deep within a module's exports, but leave all the rest of it intact.

This function makes it easy to do that.

import * from 'tap'
import * as FS from 'node:fs'

t.test('situation where we get a bogus file descriptor', async t => {
  const { thing } = await t.mockImport(
    '../dist/my-thing.js',
    { 'node:fs': t.createMock(FS, { openSync: () => true }) }
  ) as typeof import('../dist/my-thing.js')
  t.throws(() => thing(), {
    // imagine this is the error we get for some reason
    message: 'got non-numeric file descriptor: true',
  })
})

Loader

The t.mockImport() function relies on the @tapjs/mock/loader loader being used, which this plugin adds to tap's set of loaders.

If you run tests directly with node, and they use t.mockImport then you'll have to include --loader=@tapjs/mock/loader to the command line arguments ahead of the main script filename.

Current Tags

  • 1.2.5                                ...           latest (a year ago)
  • 1.0.0                                ...           pre (a year ago)

40 Versions

  • 1.2.5                                ...           a year ago
  • 1.2.4                                ...           a year ago
  • 1.2.3                                ...           a year ago
  • 1.2.2                                ...           a year ago
  • 1.2.1                                ...           a year ago
  • 1.2.0                                ...           a year ago
  • 1.1.1                                ...           a year ago
  • 1.1.0                                ...           a year ago
  • 1.0.8                                ...           a year ago
  • 1.0.7                                ...           a year ago
  • 1.0.6                                ...           a year ago
  • 1.0.5                                ...           a year ago
  • 1.0.4                                ...           a year ago
  • 1.0.3                                ...           a year ago
  • 1.0.2                                ...           a year ago
  • 1.0.1                                ...           a year ago
  • 1.0.0                                ...           a year ago
  • 0.0.0-23                                ...           a year ago
  • 0.0.0-22                                ...           a year ago
  • 0.0.0-21                                ...           a year ago
  • 0.0.0-20                                ...           a year ago
  • 0.0.0-19                                ...           a year ago
  • 0.0.0-18                                ...           a year ago
  • 0.0.0-17                                ...           a year ago
  • 0.0.0-16                                ...           a year ago
  • 0.0.0-15                                ...           a year ago
  • 0.0.0-14                                ...           a year ago
  • 0.0.0-13                                ...           a year ago
  • 0.0.0-12                                ...           a year ago
  • 0.0.0-11                                ...           a year ago
  • 0.0.0-10                                ...           a year ago
  • 0.0.0-9                                ...           a year ago
  • 0.0.0-8                                ...           a year ago
  • 0.0.0-7                                ...           a year ago
  • 0.0.0-6                                ...           a year ago
  • 0.0.0-5                                ...           a year ago
  • 0.0.0-4                                ...           a year ago
  • 0.0.0-3                                ...           a year ago
  • 0.0.0-2                                ...           a year ago
  • 0.0.0-1                                ...           a year ago
Maintainers (2)
Downloads
Today 0
This Week 0
This Month 2
Last Day 0
Last Week 2
Last Month 0
Dependencies (4)
Dev Dependencies (0)
None
Dependents (2)

Copyright 2013 - present © cnpmjs.org | Home |