UNPKG

1.23 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4require("./matchers");
5var _supportedMatchers = _interopRequireDefault(require("./supported-matchers"));
6/**
7 * Internal dependencies
8 */
9
10/**
11 * Sets spy on the console object's method to make it possible to fail test when method called without assertion.
12 *
13 * @param {Array} args
14 * @param {string} args."0" Name of console method.
15 * @param {string} args."1" Name of Jest matcher.
16 */
17const setConsoleMethodSpy = ([methodName, matcherName]) => {
18 const spy = jest.spyOn(console, methodName).mockName(`console.${methodName}`);
19
20 /**
21 * Resets the spy to its initial state.
22 */
23 function resetSpy() {
24 spy.mockReset();
25 spy.assertionsNumber = 0;
26 }
27
28 /**
29 * Verifies that the spy has only been called if expected.
30 */
31 function assertExpectedCalls() {
32 if (spy.assertionsNumber === 0 && spy.mock.calls.length > 0) {
33 expect(console).not[matcherName]();
34 }
35 }
36 beforeAll(resetSpy);
37 beforeEach(() => {
38 assertExpectedCalls();
39 resetSpy();
40 });
41 afterEach(assertExpectedCalls);
42};
43Object.entries(_supportedMatchers.default).forEach(setConsoleMethodSpy);
44//# sourceMappingURL=index.js.map
\No newline at end of file