Front-End Testing is For Everyone | CSS-Tricks

Testing is one of those things that you either get super excited about or kinda close your eyes and walk away. Whichever camp you fall into, I’m here to tell you that front-end testing is for everyone. In fact, there are many types of tests and perhaps that is where some of the initial fear or confusion comes from.
I’m going to cover the most popular and widely used types of tests in this article. This might be nothing new to some of you, but it can at least serve as a refresher. Either way, my goal is that you’re able to walk away with a good idea of the different types of tests out there. Unit. Integration. Accessibility. Visual regression. These are the sorts of things we’ll look at together.
And not just that! We’ll also point out the libraries and frameworks that are used for each type of test, like Mocha. Jest, Puppeteer, and Cypress, among others. And don’t worry — I’ll avoid a bunch of technical jargon. That said, you should have some front-end development experience to understand the examples we’re going to cover.

OK, let’s get started!

What is testing?
Software testing is an investigation conducted to provide stakeholders with information about the quality of the software product or service under test.Cem Kaner, “Exploratory Testing” (November 17, 2006)
At its most basic, testing is an automated tool that finds errors in your development as early as possible. That way, you’re able to fix those issues before they make it into production. Tests also serve as a reminder that you may have forgotten to check your own work in a certain area, say accessibility.
In short, front-end testing validates that what people see on the site and the features they use on it work as intended.
Front-end testing is for the client side of your application. For example, front-end tests can validate that pressing a “Delete” button properly removes an item from the screen. However, it won’t necessarily check if the item was actually removed from the database — that sort of thing would be covered during back-end testing.
That’s testing in a nutshell: we want to catch errors on the client side and fix them before code is deployed.
Different tests look at different parts of the project
Different types of tests cover different aspects of a project. Nevertheless, it is important to differentiate them and understand the role of each type. Confusing which tests do what makes for a messy, unreliable testing suit.
Ideally, you’d use several different types of tests to surface different types of possible issues. Some test types have a test coverage analytic that shows just how much of your code (as a percentage) is looked at by that particular test. That’s a great feature, and while I’ve seen developers aim for 100% coverage, I wouldn’t rely on that metric alone. The most important thing is to make sure all possible edge cases are covered and taken into account.
So, with that, let’s turn our attention to the different types of testing. Remember, it’s not so much that you’re expected to use each and every one of these. It’s about being able to differentiate the tests so that you know which ones to use in certain circumstances.
Unit testing
Unit testing is the most basic building block for testing. It looks at individual components and ensures they work as expected. This sort of testing is crucial for any front-end application because, with it, your components are tested against how they’re expected to behave, which leads to a much more reliable codebase and app. This is also where things like edge cases can be considered and covered.
Unit tests are particularly great for testing APIs. But rather than making calls to a live API, hardcoded (or “mocked”) data makes sure that your test runs are always consistent at all time.
Let’s take a super simple (and primitive) function as an example:
const sayHello = (name) => {
if (!name) {
return “Hello human!”;
}

return `Hello ${name}!`;
};
Again, this is a basic case, but you can see that it covers a small edge case where someone may have neglected to provide a first name to the application. If there’s a name, we’ll get “Hello ${name}!” where ${name} is what we expect the person to have provided.
“Um, why do we need to test for something small like that?” you might wonder. There are some very important reasons for this:
It forces you to think deeply about the possible outcomes of your function. More often than not, you really do discover edge cases which helps you cover them in your code.Some part of your code can rely on this edge case, and if someone comes and deletes something important, the test will warn them that this code is important and cannot be removed.Unit tests are often small and simple. Here’s an example:
describe(“sayHello function”, () => {
it(“should return the proper greeting when a user doesn’t pass a name”, () => {
expect(sayHello()).toEqual(“Hello human!”)
})

it(“should return the proper greeting with the name passed”, () => {
expect(sayHello(“Evgeny”)).toEqual(“Hello Evgeny!”)
})
})
describe and it are just syntactic sugar. The most important lines with expect and toEqual. describe and it breaks the test into logical blocks that are printed to the terminal. The expect function accepts the input we want to validate, while toEqual accepts the desired output. There are a lot of different functions and methods you can use to test your application.
Let’s say we’re working with Jest, a library for writing units. In the example above, Jest will display the sayHello function as a title in the terminal. Everything inside an it function is considered as a single test and is reported in the terminal below the function title, making everything very easy to read.
The green checkmarks mean both of our tests have passed. Yay!Integration testing
If unit tests check the behavior of a block, integration tests make sure that blocks work flawlessly together. That makes Integration testing super important because it opens up testing interactions between components. It’s very rare (if ever) that an application is composed of isolated pieces that function by themselves. That’s why we rely on integration tests.
We go back to the function we unit tested, but this time use it in a simple React application. Let’s say that clicking a button triggers a greeting to appear on the screen. That means a test involves not only the function but also the HTML DOM and a button’s functionality. We want to test how all these parts play together.
Here’s the code for a component we’re testing:
export const Greeting = () => {
const [showGreeting, setShowGreeting] = useState(false);

return (

{showGreeting && sayHello()}

);
};
Here’s the integration test:
describe(‘‘, () => {
it(‘shows correct greeting’, () => {
const screen = render();
const greeting = screen.getByTestId(‘greeting’);
const button = screen.getByTestId(‘show-greeting-button’);

expect(greeting.textContent).toBe(”);
fireEvent.click(button);
expect(greeting.textContent).toBe(‘Hello human!’);
});
});
We already know describe and it from our unit test. They break tests up into logical parts. We have the render function that displays a component in the special emulated DOM so we can test interactions with the component without touching the real DOM — otherwise, it can be costly.
Next up, the test queries

and

Share your love

8 Comments

  1. Affiliate marketing doesn’t clear quickly. By placing your links carefully and consistently,
    you will slowly create residual dollars. A link that you placed a long term ago, will contknue to brring you income for
    years, anyone put it in best place, with good content and good referral again links.

    Befode start your blog, focus on the fundamental principle called “planning”.

    Like any other project, blogs require preparation in order
    to be deemed as a hit. The best pplan guarantees a promising future.
    The theme of the blog is a very important decision, because once you pick it, all another optrion for
    protechting steps will be based on this item.

    Once anyone could have your nichhe selected you should put up a website online.

    This is your retail store front so people inside your niche come across you.
    There are millions of web site solutions should you not know thhe way
    too do it yourself. Might want to remember option has been a wordpress blog for
    the web content. The search engines love wordpress blogs and you will probably be able to dig up learn more free traffic faster by using a blog.

    The third part iin the press release is system needs.
    The body can coming from twwo paragraphs to a website in time-span. The goal of the body
    of the press release is give readerrs enough information about oour services or products without giving too much detail, therefore the only opportunity for them come across the information would like from uus is to check out oour online.

    Evereything else cann be improved upon with practice, experience or by paying
    somone else to carry out the writing tto bbe able too if you need
    to! But the content.that is baically that you.that is where your experience,
    your knowledge comes throughout. If you give good content the reader really won’t care when you have
    a run-on sentence.

    Keywords. Use keywords foor your special topic with your headline, frequen lowering and raisin paragraphs.

    Provided you can sorinkle them throughout polst that is great, however it is better
    to write naturally theen spending plenty of time shopping force in keywords!

    Comments on the blog furthermore a extremely aspect of blog marketing communications.
    You will want to be certai that you always answer and acknowledge the comments that are created too your
    blog aas well as people today. When you show that you are an actual persn requred hold a conversation, you
    are showing others that arre usually reall so you really and truly contain the
    best intentions for your blog. Make sure that no comment goes un-answered.

    So, do these three steps: Focus, Content, and Links seem
    pretty as well as mundane? A person to some readers, yett they are essential to online success with the Autopilot Cash Cycler.
    Do what obtain to move your business rolling and getting the cash by following these housing – Focus, Content, and Links.

  2. I’m еxtremely inspired with youг writing talents and also with the structure on yoᥙr blog.
    Is that thіs a pɑid toρic or did you modify it your self?
    Anyway stаy up the excellent high quality writing, it’s uncоmmon t᧐ peer a nice blog
    like this one today..

  3. Remarkable issues here. I am very glad to look your post. Thanks so much
    aand I’m looking ahead to touch you. Will you
    kindly drop me a mail?

    Feel free to viosit my website: ace333 demo id

  4. Great blog you have here but I was curious if you knew of
    any message boards that cover the same topics discussed here?

    I’d really like to be a part of group where I can get advice from other
    experienced people that share the same interest.

    If you have any recommendations, please let me know. Bless you!

  5. is William Hill live casino Rigged
    is William Hill live casino Rigged

    Hi I am so glad I found your webpage, I really
    found you by mistake, while I was researching on Bing for something else, Regardless I am
    here now and would just like to say kudos for a incredible post and a all round enjoyable blog (I also love
    the theme/design), I don’t have time to read it all at the moment but I have bookmarked it and
    also included your RSS feeds, so when I have time I will
    be back to read a great deal more, Please do keep up the superb work.

  6. Ꭲhanks for one’s marvelous posting! I certainly еnjⲟyed reading it, you’re a great author.I will remember to bߋokmark yߋur blog and
    will eventually come back at sоme point. І want to encourage you to definitely
    continue your great job, hɑve a nice afternoon!

  7. Great weblog right here! Also your web site lots up very
    fast! What host are you the use of? Can I get
    your affiliate hyperlink for your host? I wish my site loaded up as
    quickly as yours lol

  8. I need to to thank you for this excellent read!!

    I certainly enjoyed every bit of it. I’ve got you book-marked to check out new things you post…

Leave a Reply