Skip to content

Commit ededa6a

Browse files
authored
Merge pull request #93 from stalniy/feat/fn-body
feat(spy): adds original function body to be exposed in spy.toString()
2 parents 9a2d248 + 9b599ca commit ededa6a

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed

chai-spies.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ var spy = function (chai, _) {
2121
, i = _.inspect
2222
, STATE_KEY = typeof Symbol === 'undefined' ? '__state' : Symbol('state')
2323
, spyAmount = 0
24-
, DEFAULT_SANDBOX = new Sandbox();
24+
, DEFAULT_SANDBOX = new Sandbox()
25+
, noop = function () {};
2526

2627
/**
2728
* # Sandbox constructor (function)
@@ -189,7 +190,7 @@ var spy = function (chai, _) {
189190
name = undefined;
190191
}
191192

192-
fn = fn || function () {};
193+
fn = fn || noop;
193194

194195
function makeProxy (length, fn) {
195196
switch (length) {
@@ -224,7 +225,7 @@ var spy = function (chai, _) {
224225
if (l > 0)
225226
s += ", " + l + " call" + (l > 1 ? 's' : '');
226227
s += " }";
227-
return s;
228+
return s + (fn !== noop ? "\n" + fn.toString() : '');
228229
};
229230

230231
proxy.__spy = {

lib/spy.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ module.exports = function (chai, _) {
1616
, STATE_KEY = typeof Symbol === 'undefined' ? '__state' : Symbol('state')
1717
, spyAmount = 0
1818
, DEFAULT_SANDBOX = new Sandbox()
19+
, noop = function () {}
1920

2021
/**
2122
* # Sandbox constructor (function)
@@ -183,7 +184,7 @@ module.exports = function (chai, _) {
183184
name = undefined;
184185
}
185186

186-
fn = fn || function () {};
187+
fn = fn || noop;
187188

188189
function makeProxy (length, fn) {
189190
switch (length) {
@@ -218,7 +219,7 @@ module.exports = function (chai, _) {
218219
if (l > 0)
219220
s += ", " + l + " call" + (l > 1 ? 's' : '');
220221
s += " }";
221-
return s;
222+
return s + (fn !== noop ? "\n" + fn.toString() : '');
222223
};
223224

224225
proxy.__spy = {

test/spies.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,32 @@ describe('Chai Spies', function () {
2929
it('should print out nice', function() {
3030
chai.spy().toString().should.equal("{ Spy }");
3131
});
32+
3233
it('should show the name', function() {
3334
chai.spy('Nikita').toString().should.equal("{ Spy 'Nikita' }");
3435
});
36+
3537
it('should expose number of invokations', function() {
3638
var spy = chai.spy()
3739
spy(); // 1
3840
spy(); // 2
3941
spy.toString().should.equal("{ Spy, 2 calls }");
4042
});
43+
4144
it('should expose name and number of invokations', function() {
4245
var spy = chai.spy('Nikita')
4346
spy(); // 1
4447
spy.toString().should.equal("{ Spy 'Nikita', 1 call }");
4548
});
4649

50+
it('should expose original function `toString` representation', function() {
51+
function test(a, b, c) {
52+
return a + b + c;
53+
}
54+
55+
var spy = chai.spy(test);
56+
spy.toString().should.equal("{ Spy }\n" + test.toString());
57+
});
4758
});
4859

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

0 commit comments

Comments
 (0)