Skip to content

Commit

Permalink
Merge pull request #93 from stalniy/feat/fn-body
Browse files Browse the repository at this point in the history
feat(spy): adds original function body to be exposed in spy.toString()
  • Loading branch information
meeber committed Jan 10, 2018
2 parents 9a2d248 + 9b599ca commit ededa6a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
7 changes: 4 additions & 3 deletions chai-spies.js
Expand Up @@ -21,7 +21,8 @@ var spy = function (chai, _) {
, i = _.inspect
, STATE_KEY = typeof Symbol === 'undefined' ? '__state' : Symbol('state')
, spyAmount = 0
, DEFAULT_SANDBOX = new Sandbox();
, DEFAULT_SANDBOX = new Sandbox()
, noop = function () {};

/**
* # Sandbox constructor (function)
Expand Down Expand Up @@ -189,7 +190,7 @@ var spy = function (chai, _) {
name = undefined;
}

fn = fn || function () {};
fn = fn || noop;

function makeProxy (length, fn) {
switch (length) {
Expand Down Expand Up @@ -224,7 +225,7 @@ var spy = function (chai, _) {
if (l > 0)
s += ", " + l + " call" + (l > 1 ? 's' : '');
s += " }";
return s;
return s + (fn !== noop ? "\n" + fn.toString() : '');
};

proxy.__spy = {
Expand Down
5 changes: 3 additions & 2 deletions lib/spy.js
Expand Up @@ -16,6 +16,7 @@ module.exports = function (chai, _) {
, STATE_KEY = typeof Symbol === 'undefined' ? '__state' : Symbol('state')
, spyAmount = 0
, DEFAULT_SANDBOX = new Sandbox()
, noop = function () {}

/**
* # Sandbox constructor (function)
Expand Down Expand Up @@ -183,7 +184,7 @@ module.exports = function (chai, _) {
name = undefined;
}

fn = fn || function () {};
fn = fn || noop;

function makeProxy (length, fn) {
switch (length) {
Expand Down Expand Up @@ -218,7 +219,7 @@ module.exports = function (chai, _) {
if (l > 0)
s += ", " + l + " call" + (l > 1 ? 's' : '');
s += " }";
return s;
return s + (fn !== noop ? "\n" + fn.toString() : '');
};

proxy.__spy = {
Expand Down
11 changes: 11 additions & 0 deletions test/spies.js
Expand Up @@ -29,21 +29,32 @@ describe('Chai Spies', function () {
it('should print out nice', function() {
chai.spy().toString().should.equal("{ Spy }");
});

it('should show the name', function() {
chai.spy('Nikita').toString().should.equal("{ Spy 'Nikita' }");
});

it('should expose number of invokations', function() {
var spy = chai.spy()
spy(); // 1
spy(); // 2
spy.toString().should.equal("{ Spy, 2 calls }");
});

it('should expose name and number of invokations', function() {
var spy = chai.spy('Nikita')
spy(); // 1
spy.toString().should.equal("{ Spy 'Nikita', 1 call }");
});

it('should expose original function `toString` representation', function() {
function test(a, b, c) {
return a + b + c;
}

var spy = chai.spy(test);
spy.toString().should.equal("{ Spy }\n" + test.toString());
});
});

it('should return the value of the mock function', function() {
Expand Down

0 comments on commit ededa6a

Please sign in to comment.