Skip to content

Instantly share code, notes, and snippets.

@paolocarrasco
Last active December 28, 2015 15:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paolocarrasco/7522670 to your computer and use it in GitHub Desktop.
Save paolocarrasco/7522670 to your computer and use it in GitHub Desktop.
A way to solve the problem of JSON.stringify "serialize cyclic structures" in mocha-phantomjs (https://github.com/metaskills/mocha-phantomjs/issues/104). Required only if you're printing in the console objects with cyclic structures (not very common).
describe('functionalityToTest', function() {
var stringify = JSON.stringify;
before(function() {
JSON.stringify = function(obj) {
var seen = [];
return stringify(obj, function(key, val) {
if (typeof val == "object") {
if (seen.indexOf(val) >= 0) { return; }
seen.push(val);
}
return val;
});
};
});
after(function() {
JSON.stringify = stringify;
});
// tests go here
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment