Matchers
The cli-testing-library
provides a set of custom Jest and Vitest matchers that you can
use to extend Jest or Vitest. These will make your tests more declarative, clear to read
and to maintain.
Usage
Import cli-testing-library/jest
, cli-testing-library/jest-globals
, or cli-testing-library/vitest
once, based on your testing (for instance in your
tests setup file)
and you’re good to go:
With TypeScript
If you’re using TypeScript, make sure your setup file is a .ts
and not a .js
to include the necessary types.
You will also need to include your setup file in your tsconfig.json
if you
haven’t already:
Custom matchers
toBeInTheConsole
This allows you to assert whether an instance is present or not. Useful when
combined with queries (such as getByText
or getByError
) that return the
TestInstance
Examples
toHaveErrorMessage
This allows you to check whether the given instance has an stderr
message or
not.
Whitespace is normalized.
When a string
argument is passed through, it will perform a whole
case-sensitive match to the error message text.
To perform a case-insensitive match, you can use a RegExp
with the /i
modifier.
To perform a partial match, you can pass a RegExp
.