UNPKG

stubfn

Version:

A minimal, zero-dependency stub utility for JavaScript testing. Simple API, predictable behavior, and no magic—perfect for replacing Sinon in modern test setups.

109 lines • 5.08 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const hoare_1 = require("hoare"); const stub_1 = require("./stub"); (0, hoare_1.test)('stub records calls', (assert) => { const fn = (0, stub_1.stub)(); fn('a', 1); fn('b', 2); const calls = fn.getCalls(); assert.equal(calls.length, 2); assert.equal(fn.getNumCalls(), 2); assert.equal(calls[0], ['a', 1]); assert.equal(calls[1], ['b', 2]); }); (0, hoare_1.test)('stub.clearCalls() empties call history', (assert) => { const fn = (0, stub_1.stub)(); fn.expects('x'); fn('x'); assert.equal(fn.getCalls(), [['x']]); fn.clearCalls(); assert.equal(fn.getNumCalls(), 0); }); (0, hoare_1.test)('stub.reset() returns stub to its initial state', (assert) => { const fn = (0, stub_1.stub)() .returns('ok') .when([1, 2], 'ok') .when([3, 4], 'not ok'); assert.equal(fn('blah'), 'ok', 'returns default value'); assert.equal(fn(1, 2), 'ok', 'returns matching case'); assert.equal(fn.getCalls(), [ ['blah'], [1, 2], ], 'returns call history'); // now let's reset it, set different values and check its state fn.reset().expects('The Keymaster'); assert.equal(fn.getNumCalls(), 0, 'should have empty call history'); assert.throws(() => fn('Dr. Venkman'), /Stub called with unexpected arguments/, 'should throw when called with unexpected args'); }); (0, hoare_1.test)('stub.returns() returns the stub, and sets the return value of the stub', (assert) => { const fn = (0, stub_1.stub)().returns('ok'); const result = fn(); assert.equal(result, 'ok'); }); (0, hoare_1.test)('stub throws on unexpected arguments', (assert) => { const fn = (0, stub_1.stub)().expects('expected', 42); assert.throws(() => fn('wrong', 42), /Stub called with unexpected arguments/); assert.throws(() => fn(), /Stub called with unexpected arguments/); () => fn('expected', 42); // Should not throw }); (0, hoare_1.test)('stub does recursive, strict, compare-by-value equality check for expected args', (assert) => { const fn = (0, stub_1.stub)().expects({ a: 1 }); assert.throws(() => fn({ a: 1, b: 2 }), /Stub called with unexpected arguments/); fn({ a: 1 }); // Should not throw const obj = { a: 1 }; fn(obj); // Should not throw fn.expects(2); assert.throws(() => fn(3), /Stub called with unexpected arguments/); fn(2); // Should not throw }); (0, hoare_1.test)('unexpected arg error contains name if supplied in constructor', (assert) => { const fn = (0, stub_1.stub)('my-awesome-stub').expects('expected', 42); assert.throws(() => fn('wrong', 42), /Stub "my-awesome-stub" called with unexpected arguments/); assert.throws(() => fn(), /Stub "my-awesome-stub" called with unexpected arguments/); }); (0, hoare_1.test)('stub throws error if .throws() is called with an Error', (assert) => { const fn = (0, stub_1.stub)('my-awesome-stub').throws(new Error('boom')); assert.throws(() => fn(), /boom/); }); (0, hoare_1.test)('stub throws error if .returns is called with an Error', (assert) => { const fn = (0, stub_1.stub)('my-awesome-stub').returns(new Error('boom')); assert.throws(() => fn(), /boom/); }); (0, hoare_1.test)('if return value is a function, it is called with the arguments', (assert) => { const fn = (0, stub_1.stub)('my-awesome-stub').returns((a, b) => a + b); assert.equal(fn(1, 2), 3); }); (0, hoare_1.test)('stub.when() sets up a return value for a specific set of arguments', (assert) => { const fn = (0, stub_1.stub)().when([1, 2], 'ok'); const fn2 = (0, stub_1.stub)().when([3, 4], 'not ok'); const fn3 = (0, stub_1.stub)().when(['foo'], 'bar'); assert.equal(fn(1, 2), 'ok'); assert.equal(fn2(3, 4), 'not ok'); assert.equal(fn3('foo'), 'bar'); }); (0, hoare_1.test)('stub.when() throws error if .expects() is set', (assert) => { assert.throws(() => (0, stub_1.stub)().expects('expected').when([1, 2], 'ok'), /Cannot use when\(\) after expects\(\)/); }); (0, hoare_1.test)('stub.expects() throws error if .when() is set', (assert) => { assert.throws(() => (0, stub_1.stub)().when([1, 2], 'ok').expects('expected'), /Cannot use expects\(\) after when\(\)/); }); (0, hoare_1.test)('stub.when() can be chained multiple times and used with returns .returns() as default return value', (assert) => { const fn = (0, stub_1.stub)() .returns('default') .when([1, 2], 'ok') .when([3, 4], 'not ok') .when(['foo'], 'bar'); assert.equal(fn(1, 2), 'ok'); assert.equal(fn(3, 4), 'not ok'); assert.equal(fn('foo'), 'bar'); assert.equal(fn('something else'), 'default'); }); (0, hoare_1.test)('stub returns undefined if no return value is set', (assert) => { const fn = (0, stub_1.stub)().when(['foo'], 'bar'); assert.equal(fn('foo'), 'bar'); assert.equal(fn(1, 2), undefined); assert.equal(fn(3, 4), undefined); assert.equal(fn('something else'), undefined); }); //# sourceMappingURL=stub.spec.js.map