Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(spy): adds original function body to be exposed in spy.toString() #93

Merged
merged 1 commit into from Jan 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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