Skip to content

Commit

Permalink
Merge f9b9d68 into 06a44cc
Browse files Browse the repository at this point in the history
  • Loading branch information
depfu[bot] committed Nov 6, 2021
2 parents 06a44cc + f9b9d68 commit e0589df
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 3 deletions.
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -55,7 +55,8 @@
"prettier": "^2.2.0",
"rsvp": "^4.8.2",
"serve": "*",
"sinon": "^9.0.0",
"sinon": "^12.0.1",
"sinon9": "npm:sinon@^9.2.4",
"unexpected": "^12.0.0",
"unexpected-documentation-site-generator": "^7.0.1"
},
Expand Down
90 changes: 90 additions & 0 deletions test/monkeyPatchSinon9StackFrames.js
@@ -0,0 +1,90 @@
/* global define */
// Monkey-patch sinon.create to patch all created spyCall instances
// so that the top stack frame is a predictable string.
// Prevents every test from failing when the test suite is updated.

// Use an UMD wrapper so it can be used in both node.js and Phantom.JS
(function (root, factory) {
if (typeof exports === 'object') {
factory(require('sinon9'));
} else if (typeof define === 'function' && define.amd) {
define(['sinon9'], factory);
} else {
factory(root.sinon);
}
})(this, function (sinon) {
function isSpy(value) {
return (
value &&
typeof value.id === 'string' &&
/^(?:spy|stub|fake)#/.test(value.id)
);
}

var bogusStack =
'Error\n' +
'at theFunction (theFileName:xx:yy)\n' +
'at theFunction (theFileName:xx:yy)\n' +
'at theFunction (theFileName:xx:yy)';

function patchCall(call) {
if (call) {
call.stack = bogusStack;
if (call.errorWithCallStack) {
call.errorWithCallStack = { stack: bogusStack };
}
}
return call;
}

function patchSpy(spy) {
var getCall = spy.getCall;
spy.getCall = function () {
var call = getCall.apply(spy, arguments);
return patchCall(call);
};
var getCalls = spy.getCalls;
spy.getCalls = function () {
var calls = getCalls.apply(spy, arguments);
return calls.map(patchCall, this);
};
}

function replace(name, obj) {
var orig = obj[name];
obj[name] = function () {
// ...
var result = orig.apply(this, arguments);
if (isSpy(result)) {
patchSpy(result);
}
return result;
};
obj[name].create = orig.create;
}
['spy', 'stub'].forEach(function (name) {
replace(name, sinon);
});

if (sinon.createSandbox) {
var originalCreateSandbox = sinon.createSandbox;
sinon.createSandbox = function () {
var sandbox = originalCreateSandbox.apply(this, arguments);
replace('spy', sandbox);
replace('stub', sandbox);
return sandbox;
};
}

var origCreateStubInstance = sinon.createStubInstance;
sinon.createStubInstance = function () {
// ...
var instance = origCreateStubInstance.apply(this, arguments);
for (var propertyName in instance) {
if (isSpy(instance[propertyName])) {
patchSpy(instance[propertyName]);
}
}
return instance;
};
});
4 changes: 2 additions & 2 deletions test/tests.html
Expand Up @@ -9,8 +9,8 @@
<script src="../node_modules/mocha/mocha.js"></script>
<script>mocha.setup('bdd')</script>
<script src="../node_modules/unexpected/unexpected.js"></script>
<script src="../node_modules/sinon/pkg/sinon.js"></script>
<script src="monkeyPatchSinonStackFrames.js"></script>
<script src="../node_modules/sinon9/pkg/sinon.js"></script>
<script src="monkeyPatchSinon9StackFrames.js"></script>
<script src="../lib/unexpected-sinon.js"></script>
<script>
unexpected = weknowhow.expect.clone();
Expand Down

0 comments on commit e0589df

Please sign in to comment.