Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
DOM Testing Library
3.
Things to avoid with Testing Library
4.
Guiding Principles
5.
Frequently Asked Questions
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Introduction and Features, Guiding Principles

Author Toohina Barua
1 upvote

Introduction

You want to write test cases that are easy to maintain and give you high confidence that your components are functioning properly for your users. You want your tests to avoid including implementation details as part of this goal so that refactoring your components (changes to the implementation but not the functionality) doesn't break them and slow you and your team down. The core library, DOM Testing Library, is a lightweight solution for querying and interacting with DOM nodes to test web pages. So Let us dive into the article!

DOM Testing Library

The core library, DOM Testing Library, is a lightweight solution for querying and interacting with DOM nodes to test web pages (whether simulated with JSDOM/Jest or in the browser). Its main features include searching the DOM for nodes in a similar way to how a user searches for elements on a page. As a result, the library aids in ensuring that your tests give you confidence that your application will work when it is used by a real user.

Several frameworks, including React, Angular, and Vue, have been wrapped around the core library to provide ergonomic APIs. There's also a Cypress plugin that uses testing-library queries for end-to-end tests, as well as a React Native implementation.

This library is not a test runner or framework or specific to a testing framework. Any environment that provides DOM APIs, such as Jest, Mocha + JSDOM, or a real browser, can use the DOM Testing Library.

Things to avoid with Testing Library

Testing Library advises against testing implementation details such as a component's internals (though this is still possible). The library's guiding principles emphasize a focus on tests that closely resemble how users interact with your web pages.

The following implementation details should be avoided:

  • A component's internal state
  • A component's internal methods
  • A component's lifecycle methods
  • Child components

Guiding Principles

The closer your tests are to how your software is used, the more confidence you'll have in it. Only methods and utilities that encourage you to write tests that closely resemble how your web pages are used are exposed.

The following guiding principles are used to include utilities in this project:

  • If it's about rendering components, it should deal with DOM nodes rather than component instances, and dealing with component instances should be discouraged.
  • It should be useful for testing application components in the way that they would be used by the user. Because we're using a computer and often a simulated browser environment, we have to make some compromises, but utilities should encourage tests that use the components in the way they were designed to be used.
  • APIs and utility implementations should be simple and adaptable.

At the end, we want this library to be light-weight, simple, and easy to understand.

Frequently Asked Questions

  1. Where can we use DOM Testing Library?
    Any environment that provides DOM APIs, such as Jest, Mocha + JSDOM, or a real browser, can use the DOM Testing Library.
     
  2. What is your goal while testing your components?
    You want to write maintainable tests that give you high confidence that your components are working for your users.
     
  3. What is Jest?
    Jest is a fun JavaScript testing framework with a focus on ease of use. It works with Babel, TypeScript, Node, React, Angular, Vue, and other frameworks.
     
  4. What is DOM Testing Library?
    The DOM Testing Library is a very light-weight solution for testing DOM nodes.
     
  5. Can we write unit tests with this library?
    Yes! You can write unit, integration, and end-to-end tests with this library.

Conclusion

In this article, we had a look at the introduction and features, guiding principles for DOM Testing Library.
We hope that this blog has helped you enhance your knowledge and if you would like to learn more, check out our articles on Coding Ninjas Studio. Do upvote our blog to help other ninjas grow. Happy Coding!

Live masterclass