@commercetools-frontend/jest-preset-mc-app 21.15.0 → 21.17.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,23 @@
1
1
  # @commercetools-frontend/jest-preset-mc-app
2
2
 
3
+ ## 21.17.0
4
+
5
+ ### Patch Changes
6
+
7
+ - Updated dependencies []:
8
+ - @commercetools-frontend/babel-preset-mc-app@21.17.0
9
+
10
+ ## 21.16.0
11
+
12
+ ### Patch Changes
13
+
14
+ - [#2820](https://github.com/commercetools/merchant-center-application-kit/pull/2820) [`1af9292da`](https://github.com/commercetools/merchant-center-application-kit/commit/1af9292daf3a09224551383918c2d00da43bbbee) Thanks [@emmenko](https://github.com/emmenko)! - Improve output of console usage in tests
15
+
16
+ - [#2580](https://github.com/commercetools/merchant-center-application-kit/pull/2580) [`1c40c40c9`](https://github.com/commercetools/merchant-center-application-kit/commit/1c40c40c947574ba24b411c9376640bb18c489ac) Thanks [@renovate](https://github.com/apps/renovate)! - Update `@testing-library/react-hooks` package to version `8.0.0`.
17
+
18
+ - Updated dependencies []:
19
+ - @commercetools-frontend/babel-preset-mc-app@21.16.0
20
+
3
21
  ## 21.15.0
4
22
 
5
23
  ### Patch Changes
@@ -0,0 +1,123 @@
1
+ // This module is based on the package `jest-fail-on-console` but with some custom adjustments.
2
+ const util = require('util');
3
+ const colors = require('colors/safe');
4
+
5
+ const defaultErrorMessage = (methodName, bold) =>
6
+ `Expected test not to call ${bold(`console.${methodName}()`)}.\n\n` +
7
+ `If the ${methodName} is expected, test for it explicitly by mocking it out using ${bold(
8
+ 'jest.spyOn'
9
+ )}(console, '${methodName}').mockImplementation() and test that the warning occurs.`;
10
+
11
+ const failOnConsole = ({
12
+ errorMessage = defaultErrorMessage,
13
+ shouldFailOnAssert = false,
14
+ shouldFailOnDebug = false,
15
+ shouldFailOnLog = false,
16
+ shouldFailOnInfo = false,
17
+ shouldFailOnWarn = true,
18
+ shouldFailOnError = true,
19
+ skipTest,
20
+ silenceMessage,
21
+ logButNotThrowMessage,
22
+ } = {}) => {
23
+ const flushUnexpectedConsoleCalls = (
24
+ methodName,
25
+ unexpectedConsoleCallStacks
26
+ ) => {
27
+ if (unexpectedConsoleCallStacks.length > 0) {
28
+ const messages = unexpectedConsoleCallStacks.map(([stack, message]) => {
29
+ const stackLines = stack.split('\n');
30
+ return (
31
+ `${colors.red(message)}\n` +
32
+ `${stackLines
33
+ .map((line, index) => {
34
+ if (index === stackLines.length - 1) {
35
+ return colors.white(line);
36
+ }
37
+ return colors.gray(line);
38
+ })
39
+ .join('\n')}`
40
+ );
41
+ });
42
+
43
+ const message = errorMessage(methodName, colors.bold);
44
+
45
+ throw new Error(`${message}\n\n${messages.join('\n\n')}`);
46
+ }
47
+ };
48
+
49
+ const patchConsoleMethod = (methodName) => {
50
+ let originalMethod = console[methodName];
51
+
52
+ const unexpectedConsoleCallStacks = [];
53
+
54
+ const captureMessage = (format, ...args) => {
55
+ const message = util.format(format, ...args);
56
+
57
+ if (silenceMessage && silenceMessage(message, methodName)) {
58
+ return;
59
+ }
60
+
61
+ if (logButNotThrowMessage && logButNotThrowMessage(message, methodName)) {
62
+ originalMethod(format, ...args);
63
+ return;
64
+ }
65
+
66
+ // Capture the call stack now so we can warn about it later.
67
+ // The call stack has helpful information for the test author.
68
+ // Don't throw yet though b'c it might be accidentally caught and suppressed.
69
+ const { stack } = new Error();
70
+ if (stack) {
71
+ unexpectedConsoleCallStacks.push([
72
+ stack.substr(stack.indexOf('\n') + 1),
73
+ message,
74
+ ]);
75
+ }
76
+ };
77
+
78
+ const newAssertMethod = (assertion, format, ...args) => {
79
+ if (assertion) {
80
+ return;
81
+ }
82
+
83
+ captureMessage(format, ...args);
84
+ };
85
+
86
+ const newMethod =
87
+ methodName === 'assert' ? newAssertMethod : captureMessage;
88
+
89
+ const canSkipTest = () => {
90
+ const currentTestState = expect.getState();
91
+ const testName = currentTestState.currentTestName;
92
+ const testPath = currentTestState.testPath;
93
+
94
+ if (skipTest && skipTest({ testName, testPath })) return true;
95
+
96
+ return false;
97
+ };
98
+ const shouldSkipTest = canSkipTest();
99
+
100
+ beforeEach(() => {
101
+ if (shouldSkipTest) return;
102
+
103
+ console[methodName] = newMethod; // eslint-disable-line no-console
104
+ unexpectedConsoleCallStacks.length = 0;
105
+ });
106
+
107
+ afterEach(() => {
108
+ if (shouldSkipTest) return;
109
+
110
+ flushUnexpectedConsoleCalls(methodName, unexpectedConsoleCallStacks);
111
+ console[methodName] = originalMethod;
112
+ });
113
+ };
114
+
115
+ if (shouldFailOnAssert) patchConsoleMethod('assert');
116
+ if (shouldFailOnDebug) patchConsoleMethod('debug');
117
+ if (shouldFailOnLog) patchConsoleMethod('log');
118
+ if (shouldFailOnInfo) patchConsoleMethod('info');
119
+ if (shouldFailOnWarn) patchConsoleMethod('warn');
120
+ if (shouldFailOnError) patchConsoleMethod('error');
121
+ };
122
+
123
+ module.exports = failOnConsole;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@commercetools-frontend/jest-preset-mc-app",
3
- "version": "21.15.0",
3
+ "version": "21.17.0",
4
4
  "description": "Jest preset used by a MC application",
5
5
  "bugs": "https://github.com/commercetools/merchant-center-application-kit/issues",
6
6
  "repository": {
@@ -17,7 +17,7 @@
17
17
  "main": "./jest-preset.js",
18
18
  "module": "./jest-preset.js",
19
19
  "dependencies": {
20
- "@commercetools-frontend/babel-preset-mc-app": "21.15.0",
20
+ "@commercetools-frontend/babel-preset-mc-app": "21.17.0",
21
21
  "@formatjs/intl-getcanonicallocales": "^1.9.2",
22
22
  "@formatjs/intl-listformat": "^6.5.3",
23
23
  "@formatjs/intl-locale": "^2.4.47",
@@ -23,7 +23,66 @@ require('@formatjs/intl-pluralrules/locale-data/fr');
23
23
 
24
24
  const { configure: configureRtl } = require('@testing-library/react');
25
25
  const loadConfig = require('./load-config');
26
+ const failOnConsole = require('./fail-on-console');
26
27
 
27
28
  const jestConfig = loadConfig();
28
29
 
29
30
  configureRtl(jestConfig.rtlConfig);
31
+
32
+ let additionalSilencedWarnings = [];
33
+ let additionalNonThrowingWarnings = [];
34
+
35
+ function hasMatchingRegexForMessage(messages, msgRegExps) {
36
+ return msgRegExps.some((msgRegex) =>
37
+ messages.some((msg) => msgRegex.test(msg))
38
+ );
39
+ }
40
+
41
+ function shouldSilenceWarnings(...messages) {
42
+ const silencedByJestConfig = hasMatchingRegexForMessage(
43
+ messages,
44
+ jestConfig.silenceConsoleWarnings
45
+ );
46
+ const additionallySilenced = hasMatchingRegexForMessage(
47
+ messages,
48
+ additionalSilencedWarnings
49
+ );
50
+
51
+ return silencedByJestConfig || additionallySilenced;
52
+ }
53
+
54
+ function shouldNotThrowWarnings(...messages) {
55
+ const notThrowingByJestConfig = hasMatchingRegexForMessage(
56
+ messages,
57
+ jestConfig.notThrowWarnings
58
+ );
59
+ const additionallyNonThrowing = hasMatchingRegexForMessage(
60
+ messages,
61
+ additionalNonThrowingWarnings
62
+ );
63
+
64
+ return notThrowingByJestConfig || additionallyNonThrowing;
65
+ }
66
+
67
+ failOnConsole({
68
+ shouldFailOnLog: true,
69
+ shouldFailOnInfo: true,
70
+ shouldFailOnWarn: true,
71
+ shouldFailOnError: true,
72
+ silenceMessage: (message) => {
73
+ if (!process.env.CI) {
74
+ return false;
75
+ }
76
+ return shouldSilenceWarnings(message);
77
+ },
78
+ logButNotThrowMessage: (message) => {
79
+ return shouldNotThrowWarnings(message);
80
+ },
81
+ });
82
+
83
+ global.console.config = {};
84
+
85
+ global.console.config.addSilencedWarning = (rexExp) =>
86
+ additionalSilencedWarnings.push(rexExp);
87
+ global.console.config.addNonThrowingWarning = (rexExp) =>
88
+ additionalNonThrowingWarnings.push(rexExp);
package/setup-tests.js CHANGED
@@ -1,9 +1,5 @@
1
1
  const util = require('util');
2
- const colors = require('colors/safe');
3
2
  const MutationObserver = require('@sheerun/mutationobserver-shim');
4
- const loadConfig = require('./load-config');
5
-
6
- const jestConfig = loadConfig();
7
3
 
8
4
  global.window.app = {
9
5
  applicationName: 'my-app',
@@ -24,89 +20,3 @@ Object.defineProperty(window, 'TextDecoder', {
24
20
  writable: true,
25
21
  value: util.TextDecoder,
26
22
  });
27
-
28
- let additionalSilencedWarnings = [];
29
- let additionalNonThrowingWarnings = [];
30
-
31
- function hasMatchingRegexForMessage(messages, msgRegExps) {
32
- return msgRegExps.some((msgRegex) =>
33
- messages.some((msg) => msgRegex.test(msg))
34
- );
35
- }
36
-
37
- function shouldSilenceWarnings(...messages) {
38
- const silencedByJestConfig = hasMatchingRegexForMessage(
39
- messages,
40
- jestConfig.silenceConsoleWarnings
41
- );
42
- const additionallySilenced = hasMatchingRegexForMessage(
43
- messages,
44
- additionalSilencedWarnings
45
- );
46
-
47
- return silencedByJestConfig || additionallySilenced;
48
- }
49
-
50
- function shouldNotThrowWarnings(...messages) {
51
- const notThrowingByJestConfig = hasMatchingRegexForMessage(
52
- messages,
53
- jestConfig.notThrowWarnings
54
- );
55
- const additionallyNonThrowing = hasMatchingRegexForMessage(
56
- messages,
57
- additionalNonThrowingWarnings
58
- );
59
-
60
- return notThrowingByJestConfig || additionallyNonThrowing;
61
- }
62
-
63
- // setup file
64
- function logOrThrow(log, method, messages) {
65
- let warning = `@commercetools-frontend/jest-preset-mc-app: console.${method} calls should not be used in tests.`;
66
- if (process.env.CI) {
67
- if (shouldSilenceWarnings(messages)) return;
68
-
69
- log(warning, '\n', ...messages);
70
-
71
- // NOTE: That some warnings should be logged allowing us to refactor graceully
72
- // without having to introduce a breaking change.
73
- if (shouldNotThrowWarnings(messages)) return;
74
-
75
- warning = `@commercetools-frontend/jest-preset-mc-app: console.${method} calls not allowed in tests.`;
76
-
77
- throw new Error(...messages);
78
- } else {
79
- log(colors.bgYellow.black(' WARN '), warning, '\n', ...messages);
80
- }
81
- }
82
-
83
- // eslint-disable-next-line no-console
84
- const logMessage = console.log;
85
- global.console.log = (...messages) => {
86
- logOrThrow(logMessage, 'log', messages);
87
- };
88
-
89
- // eslint-disable-next-line no-console
90
- const logInfo = console.info;
91
- global.console.info = (...messages) => {
92
- logOrThrow(logInfo, 'info', messages);
93
- };
94
-
95
- // eslint-disable-next-line no-console
96
- const logWarning = console.warn;
97
- global.console.warn = (...messages) => {
98
- logOrThrow(logWarning, 'warn', messages);
99
- };
100
-
101
- // eslint-disable-next-line no-console
102
- const logError = console.error;
103
- global.console.error = (...messages) => {
104
- logOrThrow(logError, 'error', messages);
105
- };
106
-
107
- global.console.config = {};
108
-
109
- global.console.config.addSilencedWarning = (rexExp) =>
110
- additionalSilencedWarnings.push(rexExp);
111
- global.console.config.addNonThrowingWarning = (rexExp) =>
112
- additionalNonThrowingWarnings.push(rexExp);