Skip to content

Commit

Permalink
Add debugInfo method
Browse files Browse the repository at this point in the history
  • Loading branch information
jgonggrijp committed Aug 11, 2023
1 parent f323fb1 commit 8fe1bbb
Show file tree
Hide file tree
Showing 5 changed files with 251 additions and 1 deletion.
28 changes: 28 additions & 0 deletions backbone.js
Original file line number Diff line number Diff line change
Expand Up @@ -2133,5 +2133,33 @@
};
};

// Provide useful information when things go wrong.
Backbone.debugInfo = function() {
// Use the `partialRight` function as a Lodash indicator. It was never in
// Underscore, has been in Lodash at least since version 1.3.1, and is
// unlikely to be mixed into Underscore since nobody needs it.
var lodash = !!_.partialRight;
var info = {
backbone: Backbone.VERSION,
// Is this the exact released version, or a later development version?
/* This is automatically temporarily replaced when publishing a release,
so please don't edit this. */
distribution: 'MARK_DEVELOPMENT',
_: (lodash ? 'lodash ' : '') + _.VERSION,
$: !$ ? false : $.fn && $.fn.jquery ? $.fn.jquery :
$.zepto ? 'zepto' : $.ender ? 'ender' : true
};
if (typeof root.Deno !== 'undefined') {
info.deno = _.pick(root.Deno, 'version', 'build');
} else if (typeof root.process !== 'undefined') {
info.process = _.pick(root.process, 'version', 'platform', 'arch');
} else if (typeof root.navigator !== 'undefined') {
info.navigator = _.pick(root.navigator, 'userAgent', 'platform', 'webdriver');
}
/* eslint-disable-next-line no-console */
console.debug('Backbone debug info: ', JSON.stringify(info, null, 4));
return info;
};

return Backbone;
});
175 changes: 175 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"karma-qunit": "^4.1.2",
"phantomjs-prebuilt": "^2.1.7",
"qunit": "^2.17.2",
"replace-in-file": "^7.0.1",
"uglify-js": "^3.14.5"
},
"scripts": {
Expand All @@ -33,7 +34,9 @@
"alias-sourcemap": "cpy --rename=backbone-min.map backbone-min.js.map .",
"doc": "docco backbone.js && docco examples/todos/todos.js examples/backbone.localStorage.js",
"lint": "eslint backbone.js test/*.js",
"prepublishOnly": "npm run test && npm run build && npm run alias-sourcemap && npm run doc"
"mark-release": "replace-in-file MARK_DEVELOPMENT MARK_RELEASE backbone.js",
"mark-develop": "replace-in-file MARK_RELEASE MARK_DEVELOPMENT backbone.js",
"prepublishOnly": "npm run test && npm run mark-release && npm run build && npm run alias-sourcemap && npm run doc && git add -u && npm run mark-develop"
},
"main": "backbone.js",
"version": "1.4.1",
Expand Down
43 changes: 43 additions & 0 deletions test/debuginfo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/* eslint-disable no-console */

(function(QUnit) {

var logs, originalDebug = console.debug;

function spyDebug() {
logs.push(arguments);
originalDebug.apply(console, arguments);
}

QUnit.module('Backbone.debugInfo', {

beforeEach: function() {
logs = [];
console.debug = spyDebug;
},

afterEach: function() {
console.debug = originalDebug;
logs = undefined;
}
});

QUnit.test('debugInfo', function(assert) {
var info = Backbone.debugInfo();
assert.strictEqual(info.backbone, Backbone.VERSION, 'includes Backbone version');
assert.strictEqual(info.distribution, 'MARK_DEVELOPMENT', 'distribution mark sticks to development');
assert.strictEqual(info._, _.VERSION, 'includes Underscore version');
assert.strictEqual(info.$, $.fn.jquery, 'includes jQuery version');
if (typeof navigator !== 'undefined') {
assert.ok(typeof info.navigator === 'object');
assert.strictEqual(info.navigator.userAgent, navigator.userAgent, 'includes user agent');
assert.strictEqual(info.navigator.platform, navigator.platform, 'includes navigator platform');
assert.strictEqual(info.navigator.webdriver, navigator.webdriver, 'includes webdriver state');
}
assert.strictEqual(logs.length, 1, 'prints to console as side effect');
var debugArgs = logs[0];
var infoString = JSON.stringify(info, null, 4);
assert.strictEqual(debugArgs[1], infoString, 'prints payload as second argument');
});

})(QUnit);
1 change: 1 addition & 0 deletions test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<script src="setup/dom-setup.js"></script>
<script src="setup/environment.js"></script>
<script src="noconflict.js"></script>
<script src="debuginfo.js"></script>
<script src="events.js"></script>
<script src="model.js"></script>
<script src="collection.js"></script>
Expand Down

0 comments on commit 8fe1bbb

Please sign in to comment.