You want to use Jasmine to write tests that assert various things about the state of the DOM. As part of that goal, you want to avoid all the repetitive patterns that arise in doing so. Checking for an element's attributes, its text content, its css classes, you name it.
The jasmine-dom
library provides a set of custom Jasmine matchers that you can use to extend Jasmine. These will make your tests more declarative, clear to read and to maintain.
- Installation
- Usage
- Documentation
- Inspiration
- Other Solutions
- Guiding Principles
- Contributors
- LICENSE
This module is distributed via npm which is bundled with node and should be installed as one of your project's devDependencies
.
Using npm:
npm install --save-dev jasmine-dom
or for installation using yarn package manager:
yarn add --dev jasmine-dom
You should have a directory for helpers specified inside the helpers array in your jasmine.json
file.
Example:
{
"spec_dir": "src/__tests__",
"spec_files": ["**/*.test.js"],
"helpers": ["helpers/**/*.js"],
"stopSpecOnExpectationFailure": false,
"random": false
}
Make a new file inside that directory, import jasmine-dom and add the matchers like so:
import JasmineDOM from 'jasmine-dom';
beforeAll(() => {
jasmine.getEnv().addMatchers(JasmineDOM);
});
That's it! You're good to go.
This library is meant to be a Jasmine version of @testing-library/jest-dom
library. As such, it provides the same set of matchers and the same functionality for each one.
You can find examples for each matcher on jest-dom's docs.
This library was heavily inspired by testing-library being jest-dom a part of its ecosystem, and Kent C. Dodds' guiding principles.
The intention is to make these matchers available to developers using Jasmine instead of Jest.
I'm not aware of any, if you are please do make a PR and add it here!
For extending Jasmine's matchers outside the realm of DOM testing, Jasmine-Matchers is an option.
The more your tests resemble the way your software is used, the more confidence they can give you
Thanks goes to these people (emoji key)
This project follows the all-contributors specification. Contributions of any kind are welcome!
MIT