I want to write a custom assert
function for QUnit to check if an actual string matches an expected regex. With help of this question I wrote a first basic version that works as expected:
QUnit.extend(QUnit.assert, {
matches: function (actual, regex, message) {
var success = !!regex && !!actual && (new RegExp(regex)).test(actual);
var expected = "String matching /" + regex.toString() + "/";
QUnit.push(success, actual, expected, message);
}
});
QUnit.test("New assertion smoke test", function (assert) {
// force a failure to see the new assert work properly:
assert.matches("flower", "gibberish");
});
This outputs:
Message: Expected: "string matching /gibberish/", Actual: "flower"
Great!
However, while writing this I checked both the QUnit.extend
docs and the QUnit.push docs. However, the latter mentions that:
This method is deprecated and it's recommended to use it through its direct reference in the assertion context.
But I fail to see how I can apply this advice inside the QUnit.extend
context.
How do I properly write a custom assertion that doesn't use the deprecated QUnit.push
function?
push
article (the one I link to in my question) links to the new one you suggested. – Manthei