|
| 1 | +# jest-console-mock |
| 2 | + |
| 3 | +Simple wrapper to mock (and unmock) `console.log`, `.warn`, `.error` calls |
| 4 | + |
| 5 | +## What this does |
| 6 | + |
| 7 | +When testing, I wanted a way of hiding logs which I was expecting, while allowing those which I wasn't expecting to still show. |
| 8 | + |
| 9 | +This code mocks the requested `console`, when it is called the message is checked against the list of expected messages, if none match it calls `console.info` with the message. |
| 10 | + |
| 11 | + |
| 12 | +## Usage |
| 13 | + |
| 14 | +### `mockConsole(['messages'], 'log'|'warn'|'error')` |
| 15 | + |
| 16 | +_index.js_ |
| 17 | +````js |
| 18 | + console.log('message1'); //expected |
| 19 | + console.log('message2'); //expected |
| 20 | + console.log('message3'); //unexpected |
| 21 | +```` |
| 22 | + |
| 23 | +_spec.js_ |
| 24 | +````js |
| 25 | +beforeAll(() => { |
| 26 | + mockConsole(['message1', 'message2'], 'log'); |
| 27 | +}); |
| 28 | + |
| 29 | +it('should do something', () => { |
| 30 | + **** |
| 31 | +}); |
| 32 | +```` |
| 33 | + |
| 34 | +_output_ |
| 35 | +````zsh |
| 36 | +should do something: |
| 37 | + console.info **/index.js:* |
| 38 | + message3 |
| 39 | +```` |
| 40 | + |
| 41 | +### `unmockConsole('log'|'warn'|'error')` |
| 42 | + |
| 43 | +_index.js_ |
| 44 | +````js |
| 45 | + console.error('message1'); //expected |
| 46 | + console.error('message2'); //expected |
| 47 | + console.error('message3'); //unexpected |
| 48 | +```` |
| 49 | + |
| 50 | +_spec.js_ |
| 51 | +````js |
| 52 | +beforeAll(() => { |
| 53 | + mockConsole(['message1', 'message2'], 'error'); |
| 54 | +}); |
| 55 | + |
| 56 | +afterEach(() => { |
| 57 | + unmockConsole('error'); |
| 58 | +}); |
| 59 | + |
| 60 | +it('should do something', () => { |
| 61 | + **** |
| 62 | +}); |
| 63 | +it('should do something else', () => { |
| 64 | + **** |
| 65 | +}); |
| 66 | + |
| 67 | +```` |
| 68 | + |
| 69 | +_output_ |
| 70 | +````zsh |
| 71 | +should do something: |
| 72 | + console.info **/index.js:* |
| 73 | + message3 |
| 74 | + |
| 75 | +should do something else: |
| 76 | + console.error **/index.js:* |
| 77 | + message1 |
| 78 | + console.error **/index.js:* |
| 79 | + message2 |
| 80 | + console.error **/index.js:* |
| 81 | + message3 |
| 82 | +```` |
| 83 | + |
| 84 | +### `unmockAllConsoles()` |
| 85 | + |
| 86 | +_index.js_ |
| 87 | +````js |
| 88 | + console.log('message1'); //expected |
| 89 | + console.warn('message2'); //expected |
| 90 | + console.error('message3'); //expected |
| 91 | +```` |
| 92 | + |
| 93 | +_spec.js_ |
| 94 | +````js |
| 95 | +beforeAll(() => { |
| 96 | + mockConsole(['message1'], 'log'); |
| 97 | + mockConsole(['message2'], 'warn'); |
| 98 | + mockConsole(['message3'], 'error'); |
| 99 | +}); |
| 100 | + |
| 101 | +afterEach(() => { |
| 102 | + unmockAllConsoles(); |
| 103 | +}); |
| 104 | + |
| 105 | +it('should do something', () => { |
| 106 | + **** |
| 107 | +}); |
| 108 | +it('should do something else', () => { |
| 109 | + **** |
| 110 | +}); |
| 111 | + |
| 112 | +```` |
| 113 | + |
| 114 | +_output_ |
| 115 | +````zsh |
| 116 | +should do something: |
| 117 | + |
| 118 | +should do something else: |
| 119 | + console.log **/index.js:* |
| 120 | + message1 |
| 121 | + console.warn **/index.js:* |
| 122 | + message2 |
| 123 | + console.error **/index.js:* |
| 124 | + message3 |
| 125 | +```` |
0 commit comments