Strongly typed, fluent mocking for TypeScript and JavaScript, inspired by NSubstitute.
Quick start · Cheat sheet · Full guide · Migrating from 1.x · Changelog
import { Substitute, Arg } from '@fluffy-spoon/substitute'
interface Mailer {
send(to: string, subject: string): boolean
}
const mailer = Substitute.for<Mailer>()
mailer.send(Arg.any(), 'Welcome!').returns(true)
console.log(mailer.send('ann@example.com', 'Welcome!')) // prints true
mailer.received(1).send('ann@example.com', Arg.any())
mailer.didNotReceive().send('bob@example.com', Arg.any())No jest.fn() per method, no as any, no real instance needed: you write the call you expect, exactly as the code under test makes it, and the compiler checks it.
- Reads like the code it fakes. Configure with
sub.method(args).returns(value)and verify withsub.received().method(args). No stringly-typed method names. - Strongly typed. Argument types, return values and matcher predicates are all inferred from your interface or class, and
strictmode is fully supported. - Fakes from nothing. Create a substitute for any interface, class, function or constructor type. Every member is faked, so nothing real runs by accident.
- Partial mocks when you want them. Hand some or all calls to a real implementation with
mimicks()or[mimick](). - Clear failure messages. A failed check tells you what was expected, what was received, and where.
- Works with any test runner. ava, jest, mocha,
node:testand vitest are tested on every change. CommonJS, ES modules and the major bundlers are supported. - No runtime dependencies.
npm install @fluffy-spoon/substitute --save-devYou need Node.js 10 or newer (12.17+ for import) and, if you use TypeScript, TypeScript 3.6 or newer. Still on version 1? Pin it with @fluffy-spoon/substitute@1 and see the migration guide for upgrading.
Say you want to test a service that depends on a repository and a logger:
import { Substitute, Arg } from '@fluffy-spoon/substitute'
interface User { id: number; name: string }
interface UserRepository {
find(id: number): Promise<User | undefined>
save(user: User): Promise<void>
}
interface Logger {
warn(message: string): void
}
// the code under test
class UserService {
constructor(private readonly users: UserRepository, private readonly logger: Logger) { }
async rename(id: number, name: string) {
const user = await this.users.find(id)
if (!user) {
this.logger.warn(`user ${id} not found`)
return false
}
await this.users.save({ ...user, name })
return true
}
}
// 1. create substitutes
const users = Substitute.for<UserRepository>()
const logger = Substitute.for<Logger>()
// 2. configure what they answer
users.find(1).resolves({ id: 1, name: 'ann' })
users.find(Arg.is(id => id > 100)).resolves(undefined)
const service = new UserService(users, logger)Then run the code under test and check what it did:
async function test() {
// 3. exercise the code under test
console.log(await service.rename(1, 'anna')) // prints true
console.log(await service.rename(404, 'nobody')) // prints false
// 4. verify the calls it made
users.received(1).save({ id: 1, name: 'anna' })
logger.received(1).warn('user 404 not found')
logger.didNotReceive().warn('user 1 not found')
}
test()A failed check throws a SubstituteException, so it fails the test in whatever runner you use.
The examples below use this interface:
import { Substitute, Arg } from '@fluffy-spoon/substitute'
interface Calculator {
add(a: number, b: number): number
divide(a: number, b: number): number
heavyOperation(...numbers: number[]): Promise<number>
isEnabled: boolean
}
const calculator = Substitute.for<Calculator>()calculator.add(1, 2).returns(3) // every add(1, 2) returns 3
calculator.add(2, 2).returns(4, 5) // 4, then 5 from then on
calculator.isEnabled.returns(true) // properties work the same way
calculator.heavyOperation(1).resolves(42) // a resolved promise
calculator.heavyOperation(0).rejects(new Error('too light'))
calculator.divide(Arg.any(), 0).throws(new Error('cannot divide by 0'))
calculator.add(10, Arg.any()).mimicks((a, b) => a * b) // run your own implementation
console.log(calculator.add(1, 2)) // prints 3
console.log(calculator.add(2, 2), calculator.add(2, 2), calculator.add(2, 2)) // prints 4 5 5
console.log(calculator.isEnabled) // prints true
console.log(calculator.add(10, 5)) // prints 50
calculator.divide(1, 0) // throws Error: cannot divide by 0
calculator.heavyOperation(1).then(result => console.log(result)) // prints 42| Matcher | Matches |
|---|---|
Arg.any() |
any value, including undefined and null |
Arg.any('string') |
values of a type: 'string', 'number', 'boolean', 'object', 'array', 'function', ... |
Arg.is(x => x > 0) |
values for which the predicate is truthy (the parameter type is inferred) |
Arg.is.not(...), Arg.any.not(...) |
the inverse of the matcher |
Arg.all() |
any number of remaining arguments, including none |
{ id: Arg.any(), name: 'bob' } |
matchers also work inside plain objects and arrays |
expect.any(String), sinon.match(...) |
the asymmetric matchers of jest, vitest and sinon work too |
calculator.add(1, 2).returns(3)
calculator.add(Arg.is(a => a > 100), Arg.any('number')).returns(100)
calculator.add(Arg.all()).returns(0)
// the most specific setup wins: literals, then matchers, then Arg.all()
console.log(calculator.add(1, 2)) // prints 3
console.log(calculator.add(200, 1)) // prints 100
console.log(calculator.add(5, 5)) // prints 0import { clearReceivedCalls } from '@fluffy-spoon/substitute'
// the code under test
const disable = (c: Calculator) => { c.isEnabled = false }
calculator.add(1, 2)
calculator.add(1, 2)
disable(calculator)
calculator.received().add(1, 2) // at least once
calculator.received(2).add(1, 2) // exactly twice
calculator.received(2).add(Arg.all()) // matchers work in checks, too
calculator.didNotReceive().add(3, 4) // never
calculator.received().isEnabled = false // property assignments can be checked
calculator.received(1).add(1, 2) // throws SubstituteException
calculator[clearReceivedCalls]() // forget received calls, keep setups
calculator.didNotReceive().add(1, 2)Use real behaviour for some calls with mimicks(), or for everything you have not configured with [mimick]():
import { mimick } from '@fluffy-spoon/substitute'
class RealCalculator implements Calculator {
isEnabled = true
add(a: number, b: number) { return a + b }
divide(a: number, b: number) { return a / b }
async heavyOperation(...numbers: number[]) { return numbers.reduce((total, n) => total + n, 0) }
}
calculator[mimick](new RealCalculator())
calculator.divide(Arg.any(), 0).returns(NaN) // setups still win over the real instance
console.log(calculator.add(2, 3)) // prints 5
console.log(calculator.divide(1, 0)) // prints NaN
calculator.received().add(2, 3) // and calls are still recordedAnything you have not configured returns an auto-substitute, so code under test can reach through nested members without crashing:
const { Substitute } = require('@fluffy-spoon/substitute')
const app = Substitute.for()
app.getCalculator().add(1, 2).returns(99)
console.log(app.getCalculator().add(1, 2)) // prints 99
app.logger.info('started')
app.logger.received().info('started')In TypeScript, nested members are typed as their real types, so configure them by returning substitutes of your own: app.logger.returns(logger). See auto-substitutes.
Once a call is configured, writing it again calls it. To change an existing setup, use [configure]():
import { configure } from '@fluffy-spoon/substitute'
calculator.add(Arg.all()).returns(10)
calculator[configure]().add(1, 3).returns(1337)
console.log(calculator.add(1, 3)) // prints 1337
console.log(calculator.add(5, 2)) // prints 10Every method of the library is also exported as a symbol, which never clashes with your own members: received, didNotReceive, returns, throws, resolves, rejects, mimicks, mimick, configure and clearReceivedCalls.
import { Substitute, received } from '@fluffy-spoon/substitute'
interface Inbox {
received(): string[]
}
const inbox = Substitute.for<Inbox>()
inbox.received().returns(['hello'])
console.log(inbox.received()) // prints [ 'hello' ]
inbox[received](1).received()There is nothing to set up: checks throw, and runners report what throws. For example, with node:test:
import test from 'node:test'
import { Substitute } from '@fluffy-spoon/substitute'
interface Clock { now(): number }
test('reads the clock once', () => {
const clock = Substitute.for<Clock>()
clock.now().returns(1000)
const elapsed = (c: Clock, start: number) => c.now() - start
if (elapsed(clock, 400) !== 600) throw new Error('wrong elapsed time')
clock.received(1).now()
})Verify substitutes with received() and didNotReceive(), not with spy assertions like jest's toHaveBeenCalledWith() or sinon's assert.called(): a substitute is not a spy. The guide has a translation table, plus recipes for mocking a whole module with jest and vitest.
The named import works with every module system, bundler and loader:
import { Substitute, Arg } from '@fluffy-spoon/substitute'const { Substitute, Arg } = require('@fluffy-spoon/substitute')A default import (import Substitute from ...) works too, except in a few bundler setups described in Importing.
The complete guide covers every feature in depth, and every example in it is type-checked and run on every change:
- Creating substitutes for interfaces, classes, functions and constructors
- Return values, promises, exceptions and mimicking
- Argument matchers, how arguments are compared and match order
- Partial mocks, auto-substitutes and fluent interfaces
- Verifying calls and assertions that fail later
- Typing limitations and test runners
Upgrading? Read the migration guide and the changelog. For a comparison with other mocking libraries, see this article (written for version 1; the ideas still apply).
PRs are very welcome! CONTRIBUTING.md explains how the repository is laid out, how to run the tests and how releases work.
This project exists thanks to all the people who contribute.
Become a financial contributor and help us sustain the project. [Contribute]
Support this project with your organization. Your logo will show up here with a link to your website. [Contribute]
MIT © Mathias Lykkegaard Lorenzen