Add new Jest --compact-console flag for DevTools tests (#22495)
Brian Vaughn committed
Oct 5, 2021 at 11:58 UTC
cadf94df1f77a75f3cc67f284fa951c6024e51d2
2 files changed
+39
packages/react-devtools-shared/src/__tests__/setupTests.js
+20
@@ -7,11 +7,31 @@
7
* @flow
8
*/
9
10
+import {CustomConsole} from '@jest/console';
11
+
12
import type {
13
BackendBridge,
14
FrontendBridge,
15
} from 'react-devtools-shared/src/bridge';
16
17
+// Argument is serialized when passed from jest-cli script through to setupTests.
18
+const compactConsole = process.env.compactConsole === 'true';
19
+if (compactConsole) {
20
+ const formatter = (type, message) => {
21
+ switch (type) {
22
+ case 'error':
23
+ return '\x1b[31m' + message + '\x1b[0m';
24
+ case 'warn':
25
+ return '\x1b[33m' + message + '\x1b[0m';
26
+ case 'log':
27
+ default:
28
+ return message;
29
+ }
30
+ };
31
+
32
+ global.console = new CustomConsole(process.stdout, process.stderr, formatter);
33
+}
34
+
35
const env = jasmine.getEnv();
36
env.beforeEach(() => {
37
global.mockClipboardCopy = jest.fn();
scripts/jest/jest-cli.js
+19
@@ -97,6 +97,13 @@ const argv = yargs
97
requiresArg: true,
98
type: 'string',
99
},
100
+ compactConsole: {
101
+ alias: 'c',
102
+ describe: 'Compact console output (hide file locations).',
103
+ requiresArg: false,
104
+ type: 'boolean',
105
+ default: false,
106
+ },
107
}).argv;
108
109
function logError(message) {
@@ -159,6 +166,11 @@ function validateOptions() {
166
logError('DevTool tests require --build.');
167
success = false;
168
}
169
+ } else {
170
+ if (argv.compactConsole) {
171
+ logError('Only DevTool tests support compactConsole flag.');
172
+ success = false;
173
+ }
174
}
175
176
if (isWWWConfig()) {
@@ -284,6 +296,10 @@ function getEnvars() {
296
RELEASE_CHANNEL: argv.releaseChannel.match(/modern|experimental/)
297
? 'experimental'
298
: 'stable',
299
+
300
+ // Pass this flag through to the confit environment
301
+ // so the base config can conditionally load the console setup file.
302
+ compactConsole: argv.compactConsole,
303
};
304
305
if (argv.prod) {
@@ -306,7 +322,9 @@ function main() {
322
console.log(chalk.red(`\nPlease run: \`${argv.deprecated}\` instead.\n`));
323
return;
324
}
325
+
326
validateOptions();
327
+
328
const args = getCommandArgs();
329
const envars = getEnvars();
330
const env = Object.entries(envars).map(([k, v]) => `${k}=${v}`);
@@ -334,6 +352,7 @@ function main() {
352
stdio: 'inherit',
353
env: {...envars, ...process.env},
354
});
355
+
356
// Ensure we close our process when we get a failure case.
357
jest.on('close', code => {
358
// Forward the exit code from the Jest process.