https://github.com/ai/nanospy
Spy and mock methods in tests with great TypeScript support
https://github.com/ai/nanospy
Last synced: about 1 year ago
JSON representation
Spy and mock methods in tests with great TypeScript support
- Host: GitHub
- URL: https://github.com/ai/nanospy
- Owner: ai
- License: mit
- Created: 2021-11-20T21:48:24.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2023-06-21T23:45:10.000Z (almost 3 years ago)
- Last Synced: 2024-11-24T17:54:35.182Z (over 1 year ago)
- Language: TypeScript
- Homepage:
- Size: 131 KB
- Stars: 138
- Watchers: 3
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Nano Spy
A tiny Node.js library to spy and mock methods in tests
with great **TypeScript** support.
It will take only **[6 KB](https://packagephobia.com/result?p=nanospy)**
in your `node_modules` and have **0 dependencies**.
```js
import { spyOn, restoreAll } from 'nanospy'
test.after.each(() => {
restoreAll()
})
test('calls increase', () => {
const spy = spyOn(counter, 'increase')
counter.increase(5)
assert.equal(spy.callCount, 1)
assert.equal(spy.calls, [[5]])
})
```
## Usage
### Method Spy
Spy tracks method calls, but do not change method’s behavior.
```js
import { spyOn, restoreAll } from 'nanospy'
test.after.each(() => {
restoreAll()
})
test('calls increase', () => {
const spy = spyOn(counter, 'increase')
counter.increase(5)
assert.is(spy.called, true)
assert.equal(spy.callCount, 1)
assert.equal(spy.calls, [[5]])
assert.equal(spy.results, [true])
})
```
### Mock
Mock change the method’s behavior.
```js
const spy = spyOn(global, 'fetch', async () => {
return {
json: () => ({ posts })
}
})
```
Or change next function call:
```js
spy.nextResult({ ok: false })
```
```js
spy.nextError(error)
```
### Functions
`spy` can be used to track callback calls.
```js
import { spy } from 'nanospy'
const fn = spy()
fn('a', 10)
fn.callCount //=> 1
fn.calls //=> [['a', 10]]
```
You can pass `spy`’s callback:
```js
let fn = spy((name: string) => {
console.log(`Hello, ${name}!`)
})
fn('Ivan') //=> Hello, Ivan!
```
Or change next function call:
```js
fn.nextResult({ ok: false })
```
```js
fn.nextError(error)
```
### Promises
You can use helpers to test promises:
```js
import { spy } from 'nanospy'
const fn = spy()
let resolve = fn.nextResolve()
fn().then(arg => {
console.log('Resolved ' + arg)
})
await resolve(1) // => Resolved 1
```
For testing errors, you can use `fn.nextReject()`.
### Remocking
You can reassign mocked function with `onCall` method:
```js
const obj = {
mark: str => str + '!',
}
const spy = spyOn(obj, 'mark')
obj.mark('a')
assert.equal(spy.results, ['a!'])
spy.onCall(str => str + '?')
obj.mark('a')
assert.equal(spy.results, ['a!', 'a?'])
```