The cli-testing-library
provides a set of custom jest matchers that you can
use to extend jest. These will make your tests more declarative, clear to read
and to maintain.
Import cli-testing-library/extend-expect
once (for instance in your
tests setup file)
and you're good to go:
// In your own jest-setup.js (or any other name)
import 'cli-testing-library/extend-expect'
// In jest.config.js add (if you haven't already)
setupFilesAfterEnv: ['<rootDir>/jest-setup.js']
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:
// In tsconfig.json
"include": [
...
"./jest-setup.ts"
],
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
Input your name:
expect(getByText(instance, 'Input your name:')).toBeInTheConsole()
toHaveErrorMessage(text: string | RegExp)
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
.
File not found in output
expect(instance).toHaveErrorMessage('File not found in output')