From 6869a57e943391c5da016f5cebefa5613012afad Mon Sep 17 00:00:00 2001 From: Justin Wark Date: Mon, 29 Feb 2016 10:37:44 +1100 Subject: [PATCH] Improves error messages for toHaveText and toContainElement matchers, when expectation not met. --- lib/jasmine-jquery.js | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/lib/jasmine-jquery.js b/lib/jasmine-jquery.js index 266bf67c..5eeb38f3 100644 --- a/lib/jasmine-jquery.js +++ b/lib/jasmine-jquery.js @@ -512,11 +512,18 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. var actualText = $(actual).text() var trimmedText = $.trim(actualText) + var result; if (text && $.isFunction(text.test)) { - return { pass: text.test(actualText) || text.test(trimmedText) } + result = { pass: text.test(actualText) || text.test(trimmedText) } } else { - return { pass: (actualText == text || trimmedText == text) } + result = { pass: (actualText == text || trimmedText == text) } } + + result.message = result.pass ? + "Expected element '" + $(actual).html() + "' not to have text " + text : + "Expected element '" + $(actual).html() + "' to have text " + text; + + return result; } } }, @@ -554,7 +561,13 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. toContainElement: function () { return { compare: function (actual, selector) { - return { pass: $(actual).find(selector).length } + var result = { pass: $(actual).find(selector).length }; + + result.message = result.pass ? + "Expected element '" + $(actual).html() + "' not to have element " + selector : + "Expected element '" + $(actual).html() + "' to have element " + selector; + + return result; } } },