Package Exports
- tinyspy
- tinyspy/dist/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (tinyspy) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
tinyspy
minimal fork of nanospy, with more features 🕵🏻♂️
A 4KB package for minimal and easy testing with no dependencies.
This package was created for having a tiny spy library to use in vitest, but it can also be used in jest and other test environments.
Installing
// with npm
npm install -D tinyspy
// with pnpm
pnpm install -D tinyspy
// with yarn
yarn install -D tinyspyUsage
Warning! Does not support ESM mocking. You can use
tinyspywithvitest, who performs additional transformations to make ESM mocking work.
spy
Simplest usage would be:
const fn = (n: string) => n + '!'
const spied = spy(fn)
fn('a')
console.log(method.called) // true
console.log(method.callCount) // 1
console.log(method.calls) // [['a']]
console.log(method.results) // [['ok', 'a!']]
console.log(method.returns) // ['a!']You can reassign mocked function:
const fn = (n: string) => n + '!'
const spied = spy(fn).willCall((n) => n + '.')
fn('a')
console.log(method.returns) // ['a.']You can reset calls, returns, called and callCount with reset function and restore mock to it's original implementation with restore method:
const fn = (n: string) => n + '!'
const spied = spy(fn).willCall((n) => n + '.')
fn('a')
console.log(method.called) // true
console.log(method.callCount) // 1
console.log(method.calls) // [['a']]
console.log(method.returns) // ['a.']
fn.reset()
console.log(method.called) // false
console.log(method.callCount) // 0
console.log(method.calls) // []
console.log(method.returns) // []
fn.restore()
console.log(fn('a')).toBe('a!')
// still spied on!
console.log(method.callCount) // 1spyOn
All
spymethods are available onspyOn.
You can spy on an object's method or setter/getter with spyOn function.
let apples = 0
const obj = {
getApples: () => 13,
}
const spy = spyOn(obj, 'getApples', () => apples)
apples = 1
console.log(obj.getApples()) // prints 1
console.log(spy.called) // true
console.log(spy.returns) // [1]let apples = 0
let fakedApples = 0
const obj = {
get apples() {
return apples
},
set apples(count) {
apples = count
},
}
const spyGetter = spyOn(obj, { getter: 'apples' }, () => fakedApples)
const spySetter = spyOn(obj, { setter: 'apples' }, (count) => {
fakedApples = count
})
obj.apples = 1
console.log(spySetter.called) // true
console.log(spySetter.calls) // [[1]]
console.log(obj.apples) // 1
console.log(fakedApples) // 1
console.log(apples) // 0
console.log(spyGetter.called) // true
console.log(spyGetter.returns) // [1]You can even make an attribute into a dynamic getter!
let apples = 0
const obj = {
apples: 13,
}
const spy = spyOn(obj, { getter: 'apples' }, () => apples)
apples = 1
console.log(obj.apples) // prints 1You can restore spied function to it's original value with restore method:
let apples = 0
const obj = {
getApples: () => 13,
}
const spy = spyOn(obj, 'getApples', () => apples)
console.log(obj.getApples()) // 0
obj.restore()
console.log(obj.getApples()) // 13