|
| 1 | +const { getLog } = require('./'); |
| 2 | + |
| 3 | +expect(jest.isMockFunction(console.log)).toBe(false); |
| 4 | + |
| 5 | +test('simple', () => { |
| 6 | + console.log('Hello %s!', 'World'); |
| 7 | + |
| 8 | + expect(getLog().log).toMatchInlineSnapshot(` |
| 9 | + "Hello World! |
| 10 | + " |
| 11 | + `); |
| 12 | +}); |
| 13 | + |
| 14 | +test('advanced', () => { |
| 15 | + console.log( |
| 16 | + 'It works with inline value: %s, as well as floating point value: %f. Pretty cool!', |
| 17 | + 'foobar', |
| 18 | + 3.14 |
| 19 | + ); |
| 20 | + console.log( |
| 21 | + 'Map: %o,\nSet: %o,\nSymbol: %o,\nobject: %o,\nfunction: %o', |
| 22 | + new Map([['foo', 42]]), |
| 23 | + new Set([42]), |
| 24 | + Symbol('symbol'), |
| 25 | + { foo: 42 }, |
| 26 | + function foo() { |
| 27 | + return 42; |
| 28 | + } |
| 29 | + ); |
| 30 | + console.error('It works with error too'); |
| 31 | + console.warn('It works with warning too'); |
| 32 | + |
| 33 | + expect(getLog().log).toMatchInlineSnapshot(` |
| 34 | + "It works with inline value: foobar, as well as floating point value: 3.14. Pretty cool! |
| 35 | + Map: Map { 'foo' => 42, [size]: 1 }, |
| 36 | + Set: Set { 42, [size]: 1 }, |
| 37 | + Symbol: Symbol(symbol), |
| 38 | + object: { foo: 42 }, |
| 39 | + function: [Function: foo] { |
| 40 | + [length]: 0, |
| 41 | + [name]: 'foo', |
| 42 | + [prototype]: foo { [constructor]: [Circular] } |
| 43 | + } |
| 44 | + It works with error too |
| 45 | + It works with warning too |
| 46 | + " |
| 47 | + `); |
| 48 | + expect(getLog().logs).toMatchInlineSnapshot(` |
| 49 | + Array [ |
| 50 | + Array [ |
| 51 | + "log", |
| 52 | + "It works with inline value: foobar, as well as floating point value: 3.14. Pretty cool! |
| 53 | + ", |
| 54 | + ], |
| 55 | + Array [ |
| 56 | + "log", |
| 57 | + "Map: Map { 'foo' => 42, [size]: 1 }, |
| 58 | + Set: Set { 42, [size]: 1 }, |
| 59 | + Symbol: Symbol(symbol), |
| 60 | + object: { foo: 42 }, |
| 61 | + function: [Function: foo] { |
| 62 | + [length]: 0, |
| 63 | + [name]: 'foo', |
| 64 | + [prototype]: foo { [constructor]: [Circular] } |
| 65 | + } |
| 66 | + ", |
| 67 | + ], |
| 68 | + Array [ |
| 69 | + "error", |
| 70 | + "It works with error too |
| 71 | + ", |
| 72 | + ], |
| 73 | + Array [ |
| 74 | + "warn", |
| 75 | + "It works with warning too |
| 76 | + ", |
| 77 | + ], |
| 78 | + ] |
| 79 | + `); |
| 80 | + expect(getLog().levels.log).toMatchInlineSnapshot(` |
| 81 | + "It works with inline value: foobar, as well as floating point value: 3.14. Pretty cool! |
| 82 | + Map: Map { 'foo' => 42, [size]: 1 }, |
| 83 | + Set: Set { 42, [size]: 1 }, |
| 84 | + Symbol: Symbol(symbol), |
| 85 | + object: { foo: 42 }, |
| 86 | + function: [Function: foo] { |
| 87 | + [length]: 0, |
| 88 | + [name]: 'foo', |
| 89 | + [prototype]: foo { [constructor]: [Circular] } |
| 90 | + } |
| 91 | + " |
| 92 | + `); |
| 93 | + expect(getLog().levels.warn).toMatchInlineSnapshot(` |
| 94 | + "It works with warning too |
| 95 | + " |
| 96 | + `); |
| 97 | + expect(getLog().levels.error).toMatchInlineSnapshot(` |
| 98 | + "It works with error too |
| 99 | + " |
| 100 | + `); |
| 101 | +}); |
| 102 | + |
| 103 | +test('they are also mock functions', () => { |
| 104 | + console.log.mockImplementationOnce(() => {}); |
| 105 | + |
| 106 | + console.log('Will not output'); |
| 107 | + |
| 108 | + expect(console.log).toHaveBeenCalledTimes(1); |
| 109 | + expect(getLog().log).toBe(''); |
| 110 | + expect(getLog().logs).toMatchInlineSnapshot(`Array []`); |
| 111 | +}); |
0 commit comments