File: console_watcher_spec.js

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (175 lines) | stat: -rw-r--r-- 5,180 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import {
  setupConsoleWatcher,
  throwErrorFromCalls,
  forgetConsoleCalls,
  getConsoleCalls,
  ignoreConsoleMessages,
  // eslint-disable-next-line import/no-deprecated
  useConsoleWatcherThrowsImmediately,
} from './console_watcher';

const TEST_IGNORED_MESSAGE = 'Full message to ignore.';
const TEST_IGNORED_REGEX_MESSAGE = 'Part of this message matches partial ignore.';

describe('__helpers__/console_watcher', () => {
  let testEnvironment;
  let testConsole;
  let testConsoleOriginalFn;
  let consoleWatcher;

  const callConsoleMethods = () => {
    testConsole.log('Hello world log');
    testConsole.info('Hello world info');
    testConsole.info(TEST_IGNORED_MESSAGE);
    testConsole.warn(TEST_IGNORED_REGEX_MESSAGE);
    testConsole.warn('Hello world warn');
    testConsole.error('Hello world error');
    testConsole.error(TEST_IGNORED_MESSAGE);
  };

  // note: To test the beforeAll/afterAll behavior in some parts of console_watcher, we need to have our setup
  //       use beforeAll/afterAll instead of beforeEach/afterEach.
  beforeAll(() => {
    testEnvironment = { global: {} };
    testConsole = {
      log: (...args) => testConsoleOriginalFn('log', ...args),
      info: (...args) => testConsoleOriginalFn('info', ...args),
      warn: (...args) => testConsoleOriginalFn('warn', ...args),
      error: (...args) => testConsoleOriginalFn('error', ...args),
    };
    Object.defineProperty(global, 'jestConsoleWatcher', {
      get() {
        return testEnvironment.global.jestConsoleWatcher;
      },
    });
  });

  beforeEach(() => {
    // why: Let's make sure we have a fresh spy for every test
    testConsoleOriginalFn = jest.fn();
  });

  afterEach(() => {
    // why: We need to forget calls or else our main test_setup will pick up on console calls and throw an error
    forgetConsoleCalls();
  });

  describe('throwErrorFromCalls', () => {
    it('throws error with message containing calls', () => {
      const calls = [
        { method: 'error', args: ['Hello world', 2, 'Lorem\nIpsum\nDolar\nSit'] },
        { method: 'info', args: [] },
        { method: 'warn', args: ['Hello world', 'something bad happened'] },
      ];

      expect(() => throwErrorFromCalls(calls)).toThrowErrorMatchingInlineSnapshot(`
"Unexpected calls to console (3) with:
	
	[1] error: Hello world,2,Lorem
	Ipsum
	Dolar
	Sit
	
	[2] info: 
	
	[3] warn: Hello world,something bad happened
	
"
`);
    });
  });

  describe('setupConsoleWatcher', () => {
    beforeAll(() => {
      testEnvironment = { global: {} };
      consoleWatcher = setupConsoleWatcher(testEnvironment, testConsole, {
        ignores: ['Full message to ignore.', /partial ignore/],
      });
    });

    afterAll(() => {
      consoleWatcher.dispose();
    });

    describe.each(['warn', 'error'])('with %s', (method) => {
      it('with unexpected message, calls original console method', () => {
        testConsole[method]('BOOM!');

        expect(testConsoleOriginalFn).toHaveBeenCalledTimes(1);
        expect(testConsoleOriginalFn).toHaveBeenCalledWith(method, 'BOOM!');
      });

      it('with ignored message, calls original console method', () => {
        testConsole[method](TEST_IGNORED_MESSAGE);

        expect(testConsoleOriginalFn).toHaveBeenCalledTimes(1);
        expect(testConsoleOriginalFn).toHaveBeenCalledWith(method, TEST_IGNORED_MESSAGE);
      });
    });

    describe('with ignoreConsoleMessages', () => {
      ignoreConsoleMessages([/Hello world .*/]);

      it('adds to ignored messages only for describe block', () => {
        callConsoleMethods();

        expect(getConsoleCalls()).toEqual([]);
      });
    });

    describe('with useConsoleWatcherThrowsImmediately', () => {
      // eslint-disable-next-line import/no-deprecated
      useConsoleWatcherThrowsImmediately();

      it('throws when non ignored message', () => {
        expect(callConsoleMethods).toThrow();
      });
    });

    it('with getConsoleCalls, only returns non ignored ones', () => {
      expect(getConsoleCalls()).toEqual([]);

      callConsoleMethods();

      expect(getConsoleCalls()).toEqual([
        { method: 'warn', args: ['Hello world warn'] },
        { method: 'error', args: ['Hello world error'] },
      ]);
    });

    it('with forgetConsoleCalls, clears out calls', () => {
      callConsoleMethods();
      forgetConsoleCalls();

      expect(getConsoleCalls()).toEqual([]);
    });
  });

  describe('setupConsoleWatcher with shouldThrowImmediately', () => {
    beforeAll(() => {
      testEnvironment = { global: {} };
      consoleWatcher = setupConsoleWatcher(testEnvironment, testConsole, {
        ignores: ['Full message to ignore.', /partial ignore/],
        shouldThrowImmediately: true,
      });
    });

    afterAll(() => {
      consoleWatcher.dispose();
    });

    it('does not throw on ignored call', () => {
      expect(() => testConsole.error(TEST_IGNORED_MESSAGE)).not.toThrow();
    });

    it('throws when call is not ignored', () => {
      expect(() => testConsole.error('BLOW UP!')).toThrowErrorMatchingInlineSnapshot(`
"Unexpected calls to console (1) with:
	
	[1] error: BLOW UP!
	
"
`);
    });
  });
});