question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to do custom assertions

See original GitHub issue

How would one go about adding their own, project-local assertions to qunit-dom?

With QUnit, we can say

QUnit.assert.myAssertion = function() {...}

However, to be able to do that with an object returned from QUnit.dom(…), one would have to extend a DOMAssertions object or prototype. I couldn’t seem to find where that is accessible.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
lindemcommented, Jul 10, 2018

Thank you for your response. The problem is not that I don’t know how to write assertions. Also, the example is obviously bad, as people jump in recommending how to solve the particular example rather than what I really asked for.

The problem is that there are things I would have liked to assert on the node directly in a library- or project-specific manner, and I was baffled at the response that it’s just not supported and there are no plans to do so.

Even a generic thing like

assert.dom(selector).satisfies(predicate, "that DOM node satisfies this predicate")

would have been useful. But if I am the only guy who wants extensible DOM assertions I can live with qunit-dom not supporting them.

I did not intend to force the issue or come across as entitled or anything (my schedule is full as well).

Closing, I accept.

0reactions
billybonkscommented, Jul 10, 2018
assert.dom('x').hasClass('my-whatever')
assert.dom('x').hasAttribute('data-whatever', 'value')

you could make a helper function

function assertMyTwoThings(selector){
 assert.dom('x').hasClass('my-whatever')
 assert.dom('x').hasAttribute('data-whatever', 'value')
}

or

or you can extend QUnit.assert as @Turbo87 mention

QUnit.extend(QUnit.assert, {
  customDom(selector) {
    return {
       twoThingsAtOnce(){
        //this is not the real way to do the assertion but it will work
         assert.dom(selector).hasClass('my-whatever')
         assert.dom(selector).hasAttribute('data-whatever', 'value')
      }
    }
  },
});

assert.customDom('x').twoThingsAtOnce()

i would just write both assertions but if you really want to do it in one call i recommend the helper function.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Custom Assertions with AssertJ - Baeldung
Writing a custom AssertJ assertion class is pretty simple. All we need to do is to declare a class that extends AbstractAssert, add...
Read more >
13 Creating Custom Assertions
Step 1: Create the Custom Assertion Class · Step 2: Create the Custom Policy File · Step 3: Create the policy-config.xml File ·...
Read more >
Create Custom Assertions For Test Readability - Gleb Bahmutov
Create Custom Assertions For Test Readability. How to extend Cypress with custom assertions. Application; have.attr gotcha; Workarounds; Custom ...
Read more >
Custom Assertions in Java Tests - InfoQ
Custom Assertions · Both start with a call to the isNotNull() which verifies whether the tested object is not null. · They return...
Read more >
Write custom Assertions to improve your tests - Just Some Code
To write custom assertions with MSTest, write an extension method on top of the Assert class. Then, compare the expected and actual parameters ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found