Let's move to the question, we already have some in Discord, Sev was saying hi and asking if you have an opinion on dry, don't repeat yourself versus dump descriptive and meaningful phrases when it comes to end-to-end tests. In other words, if you use a code snippet in several tests, would you just copy and paste it to keep it simple or create a custom command for it? I love this question, because it's a struggle I myself feel a lot, because as a developer, of course, I like to decide in favour of the dry principle, to don't repeat myself too much. But as I said, as a tester and also as a developer, having comprehensible tests is the most important thing. Sometimes I would say don't give too much weight on dry principle and more on understand your tests. I think if I use a code snippet in several tests and it's not a big code snippet, I would use a custom command, but I would try my best to name it in a really descriptive way and to use some documentation, for example, definitions for autocompletion and stuff, which is considered a duplicate, but I think it's really important in this case, so that all people seeing this custom command do know in first sight what it is all about and what it does. So you can have it comprehensible and have some kind of dry inside of your tests. So it's I think the best thing in the middle, but I know it's really, really difficult to decide in favor from one or the other. Indeed, it's hard to be in between, but sometimes there's the good place to use it.
Liaz was wondering how to force frontend dev to use selectors. I think you mean to use selectors in favor of strings or whatever, so when it comes to selectors, it's really important to distinguish which selector you use when it comes to CSS selectors, I would rather prefer using strings or to wait for any text the user would see in your application, because CSS selectors are really prone to change. And this could be a thing to give you some failure tests, even if nothing's broken inside of your application. But if you're using other selectors, like for example, data reviews, which I myself a huge fan of. You can make it that they know they won't need to update their tests that much, because when it comes to data attributes, and especially if you name them like data CY for Cypress attributes, or any other descriptive phrase, a developer who's working on your code does know that it's meant for testing and won't change it. So your tests will fail for the right reasons and not just for an outdated class or anything like that, which is less work for the developer, right? I think that might be a wonderful argument to convince them. Definitely, we can gain them on our side with that, indeed.
Martine, oh my god, it's alive, was asking how would you compare arrange, act, assert, sorry, arrange, act, assert to given, when, then, and use which of them in which situation? Encountered any traps with this? Well, I'm so glad you ask this, Martine, because that was the thing I needed to leave out of my track because of the time frame. I wrote an article about that, that some tests in my daily doing I'm working at a huge application in the e-commerce sector, which tends to be quite conflict. And even in unit tests, it needs sometimes a bit more context in the test as you should have when it comes to unit testing. So you shouldn't call them unit testing to begin with, but when it comes to integration tests in Jest or something, you sometimes need to test the DOM, which sometimes require you to check on the before and after states of your component, for example. And in this case, arrange act assert is no good thing to use, because it's basically really difficult to press your tests inside of the structure in this case. Even when, then it's more suited in this case when it comes to integration tests or if you need some sessions on the state before and after. So you can consider it like that. Arrange my tests, I think it's what I'm giving. So all the thing you need to arrange is the given part. Act in my tests. I think that's when something happens. So that's the middle part. And assert the results.
Comments