jest-plugin-action
Declarative method for testing actions using jest.
Getting Started
Install jest-plugin-action
using yarn
:
yarn add --dev jest-plugin-action
Motivation
If you haven't already, check out jest-set
. jest-plugin-action
builds on jest-set
by providing the action
keyword which allows you to define a function. These functions are lazily defined and are useful for passing directly into expect
to test errors, warnings, etc, or for passing into beforeEach
to easily set up a scenario before defining expectations. Here's an example:
;
Even in this trivial example, it's easy to see the power of action
.
- We can declare actions in the same way we declare our variables. That keeps all of our changes in one place.
- We can create easy-to-read expectations for actions:
expect(userUser).toThrow(ValidationError);
. It doesn't get any better than this. - Using lazy actions, we can declare them in one scope and access / override variables in another scope, just like
set
!
Usage
If you want, you can import action
from jest-plugin-action
at the top of every test:
;
If you want to install action
as a global, you can modify the jest
section of your package.json
to include:
"jest":
Example
Here's an example test that tests action
itself:
;