spion
TypeScript icon, indicating that this package has built-in type declarations

0.8.0 • Public • Published

spion

Spy-on function, intended to use with the node:test functionality. Wraps the spied-upon function in an interceptor letting the function execute and return normally within its context.

Note that node:test is usable from nodejs v18.8.0, or v16.18.0 onwards.

Usage

Just call the function with the api-object, the name of the function and the execution context to get a ready-to-use object. For instance:

import createSpion, { Intelligence, Spion } from 'spion'

const mySpion: Spion = createSpion(api, 'theirMethod', window)
const returnValue = api.theirMethod(arg1, arg2)
const report: Intelligence[] = mySpion.report()

assert(
    report[0].args === [arg1, arg2] &&
    report[0].return === returnValue &&
    report[0].time < 10,
    'report should be an array of items holding arguments, return values and time after createSpion'
)

// after all tests ended
after(() => {
    mySpion.quit()
})
  • The execution context is required when testing traditional, contextual functions. (and may thus be omitted when testing arrow-functions)
  • The report method will remove the call interceptor and return a Intelligence object
  • The quit method ensures the interceptor is removed (normally not needed)

Direction

The calling arguments and the returning value can be determined.

  • The withArgs method will call the interceptor with specified arguments
  • The returnValue method will let the interceptor return with the specified value

Works as ES-module or CommonJS-module, and in the browser

Demo

/demo/spion.html

Readme

Keywords

Package Sidebar

Install

npm i spion

Weekly Downloads

0

Version

0.8.0

License

ISC

Unpacked Size

12.2 kB

Total Files

11

Last publish

Collaborators

  • ovanderzee