UNPKG

4.43 kBMarkdownView Raw
1# Jest Console
2
3Custom [Jest](https://jestjs.io/) matchers for the [Console](https://developer.mozilla.org/en-US/docs/Web/API/Console)
4object to test JavaScript code in WordPress.
5
6This package converts `console.error`, `console.info`, `console.log` and `console.warn` functions into mocks and tracks their calls.
7It also enforces usage of one of the related matchers whenever tested code calls one of the mentioned `console` methods.
8It means that you need to assert with `.toHaveErrored()` or `.toHaveErroredWith( arg1, arg2, ... )` when `console.error`
9gets executed, and use the corresponding methods when `console.info`, `console.log` or `console.warn` are called.
10Your test will fail otherwise! This is a conscious design decision which helps to detect deprecation warnings when
11upgrading dependent libraries or smaller errors when refactoring code.
12
13## Installation
14
15Install the module:
16
17```bash
18npm install @wordpress/jest-console --save-dev
19```
20
21**Note**: This package requires Node.js 14.0.0 or later. It is not compatible with older versions.
22
23### Setup
24
25The simplest setup is to use Jest's `setupFilesAfterEnv` config option:
26
27```js
28"jest": {
29 "setupFilesAfterEnv": [
30 "@wordpress/jest-console"
31 ]
32},
33```
34
35### Usage
36
37### `.toHaveErrored()`
38
39Use `.toHaveErrored` to ensure that `console.error` function was called.
40
41For example, let's say you have a `drinkAll( flavor )` function that makes you drink all available beverages.
42You might want to check if function calls `console.error` for `'octopus'` instead, because `'octopus'` flavor is really
43weird and why would anything be octopus-flavored? You can do that with this test suite:
44
45```js
46describe( 'drinkAll', () => {
47 test( 'drinks something lemon-flavored', () => {
48 drinkAll( 'lemon' );
49 expect( console ).not.toHaveErrored();
50 } );
51
52 test( 'errors when something is octopus-flavored', () => {
53 drinkAll( 'octopus' );
54 expect( console ).toHaveErrored();
55 } );
56} );
57```
58
59### `.toHaveErroredWith( arg1, arg2, ... )`
60
61Use `.toHaveErroredWith` to ensure that `console.error` function was called with
62specific arguments.
63
64For example, let's say you have a `drinkAll( flavor )` function again makes you drink all available beverages.
65You might want to check if function calls `console.error` with a specific message for `'octopus'` instead, because
66`'octopus'` flavor is really weird and why would anything be octopus-flavored? To make sure this works, you could write:
67
68```js
69describe( 'drinkAll', () => {
70 test( 'errors with message when something is octopus-flavored', () => {
71 drinkAll( 'octopus' );
72 expect( console ).toHaveErroredWith(
73 'Should I really drink something that is octopus-flavored?'
74 );
75 } );
76} );
77```
78
79### `.toHaveInformed()`
80
81Use `.toHaveInformed` to ensure that `console.info` function was called.
82
83Almost identical usage as `.toHaveErrored()`.
84
85### `.toHaveInformedWith( arg1, arg2, ... )`
86
87Use `.toHaveInformedWith` to ensure that `console.info` function was called with
88specific arguments.
89
90Almost identical usage as `.toHaveErroredWith()`.
91
92### `.toHaveLogged()`
93
94Use `.toHaveLogged` to ensure that `console.log` function was called.
95
96Almost identical usage as `.toHaveErrored()`.
97
98### `.toHaveLoggedWith( arg1, arg2, ... )`
99
100Use `.toHaveLoggedWith` to ensure that `console.log` function was called with
101specific arguments.
102
103Almost identical usage as `.toHaveErroredWith()`.
104
105### `.toHaveWarned()`
106
107Use `.toHaveWarned` to ensure that `console.warn` function was called.
108
109Almost identical usage as `.toHaveErrored()`.
110
111### `.toHaveWarnedWith( arg1, arg2, ... )`
112
113Use `.toHaveWarneddWith` to ensure that `console.warn` function was called with
114specific arguments.
115
116Almost identical usage as `.toHaveErroredWith()`.
117
118## Contributing to this package
119
120This is an individual package that's part of the Gutenberg project. The project is organized as a monorepo. It's made up of multiple self-contained software packages, each with a specific purpose. The packages in this monorepo are published to [npm](https://www.npmjs.com/) and used by [WordPress](https://make.wordpress.org/core/) as well as other software projects.
121
122To find out more about contributing to this package or Gutenberg as a whole, please read the project's main [contributor guide](https://github.com/WordPress/gutenberg/tree/HEAD/CONTRIBUTING.md).
123
124<br /><br /><p align="center"><img src="https://s.w.org/style/images/codeispoetry.png?1" alt="Code is Poetry." /></p>