Skip to content

Instantly share code, notes, and snippets.

@dfkaye
Last active March 30, 2022 22:13
Show Gist options
  • Select an option

  • Save dfkaye/c3add6603d1d3d6d2b7f9269dd896b2d to your computer and use it in GitHub Desktop.

Select an option

Save dfkaye/c3add6603d1d3d6d2b7f9269dd896b2d to your computer and use it in GitHub Desktop.
console.test() idea from mergerino-refactored gist...
// 19 June 2021
// 23 Feb 2022
// 29-30 March 2022: more ideas (bottom)
// draft idea from my mergerino refactored gist
// at https://gist.github.com/dfkaye/da82d3b7be2f97359510973a788c0285
// should add console.test.suite(name) that records subsequent
// console.test() runs (like QUnit)
// Note that console.assert() does not crash the thread.
// Could use an async/await assert()
// https://gist.github.com/dfkaye/95f3f68dc94d110a4a1f98c3b15d23ba
// Should really make console.test into a repo...
console.test = function(name, test) {
var message = "Passed"
try { test() }
catch (error) { message = "Failed: " + error }
finally { console.dir({ name, message }) }
}
console.test("deletes undefined keys", function () {
var state = {
prop: "prop",
deep: { prop: "deep" }
}
var newState = merge(state, {
prop: undefined,
deep: { prop: undefined },
undefined: undefined, // deleting non existent key
null: null
})
console.assert(Object.keys(newState).sort().join(" ") == "deep other", Object.keys(newState).join(" "))
console.assert(!("prop" in newState.deep), newState.deep.prop)
})
/* More Ideas */
// 1. overwrite console.assert with a counter function and apply arguments to real assert
var failing = 0, passing = 0;
var { assert } = console;
console.assert = function(assertion, ...rest) {
assertion ? passing++ : failing++;
assert.apply(console, [assertion, ...rest]);
return !!assertion;
}
// 2. use string#test idea 2014 instead
// see string.test gist: https://gist.github.com/dfkaye/0461d4805817c21a5609
// 3. console.test can use $, $$, $x(path) etc. for shorthand DOM element access
// see google chrome docs: https://developer.chrome.com/docs/devtools/console/utilities/#querySelector-function
// 4. console.log pretty print colors
// see parse-integer.js tests: https://gist.github.com/dfkaye/d2fb31db090fc52a7f79883d1ab1da1c#file-parse-integer-js
// 5. async-assert, rather than crashing assert
// see https://gist.github.com/dfkaye/95f3f68dc94d110a4a1f98c3b15d23ba
// 6. object#assert, rather than console.assert
// see https://gist.github.com/dfkaye/96112ac0fb956da9d8b5c8fb5255fbc3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment