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.

some/any, every, none assertions

See original GitHub issue

I’m having trouble with handling cases involving multiple nodes. My team generally doesn’t like to change the source code purely for the sake of the tests, so we can’t really use find on an id since usually we wouldn’t otherwise need an id. Therefore our tests look like this:

  context('with an adminPath passed as a prop', () => {
    it('renders a link to the admin page', () => {
      const adminPath = '/admin';
      const actual = shallow(<MyComponent {...{ adminPath }}).find({ href: adminPath });
      expect(actual).to.be.present();
    });
  });

This works, but a failing test gives us something like “expected false to be true.” We could use the enzyme helpers here such as some or someWhere, but those are really matchers, not assertions. We end up having to use .length.to.equal(1) or something similar, which doesn’t really convey the true meaning of the test.

I think it would be great if we could have some sort of API like this:

const adminPath = '/admin';
const actual = shallow(<MyComponent {...{ adminPath }});
expect(actual).any.to.haveAttr('href', adminPath);

The inverse would be:

const adminPath = '/admin';
const actual = shallow(<MyComponent {...{ adminPath }});
expect(actual).none.to.haveAttr('href', adminPath);

Is there an alternative that I missed?

If no, is this feasible? I would imagine it would involve somehow extending enzyme, as this is where the non property would have to come from. If this is possible, I’d be happy to help!

Issue Analytics

  • State:open
  • Created 8 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
marcodejonghcommented, Jan 22, 2016

You could write a assertion with descendants because it accepts a enzyme selector. I think the following would work:

const actual = expect(<MyComponent {...{ adminPath }}).to.have.descendants({ href: adminPath });

Although I’m not sure if href is a key or property in this case. It will only work for properties.

Besides that, if you want to do any kind of sane UI testing with Selenium you’re gonna be adding those same id’s & classes that would have made this unit tests easier to write. In the end the team should write testable code, having good identifiers on the elements you want to test later on is part of that.

0reactions
alex35milcommented, Jan 23, 2016

My only issue with ids is that we’re going back to the problem which was just solved by CSS Modules: you need to think about uniqueness of ids.

  1. It’s not a problem in context of component-oriented js unit testing, but it’s not standard compliant in context of document (ok, I’m a bit pedantic here, but there is # 2).
  2. If you’re using it in unit testing, you’ll be using it in integration testing, and here we have a real problem.
Read more comments on GitHub >

github_iconTop Results From Across the Web

Support failing a test if no assertions are ran #2209 - GitHub
I/we use these rules all the time, but they do not catch logic issues with non-trivial tests. Which is why we need a...
Read more >
Unit Testing without Assertions - Stack Overflow
The problem is that the test would pass if all the functions called were no-ops. But sometimes it just isn't feasible to verify...
Read more >
assertive and non-assertive forms - ELT Concourse
The any- vs. some- series of words and other assertive / non-assertive pairs · Statements Any news would be welcome. Anyone can see...
Read more >
Assertion Grammars - W3C
Documents commonly include a sequence of elements, all belonging to a given set. ... Some assertions only apply in a particular context.
Read more >
Stop requiring only one assertion per unit test
This example is in C# using xUnit.net because we need some language and ... While clearly a no-op, this implementation passes all tests....
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