Skip to content

Commit a1ef403

Browse files
Brian Vaughnzhengjitf
Brian Vaughn
authored andcommitted
Add new Jest --compact-console flag for DevTools tests (facebook#22495)
1 parent d1088fe commit a1ef403

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

packages/react-devtools-shared/src/__tests__/setupTests.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,31 @@
77
* @flow
88
*/
99

10+
import {CustomConsole} from '@jest/console';
11+
1012
import type {
1113
BackendBridge,
1214
FrontendBridge,
1315
} from 'react-devtools-shared/src/bridge';
1416

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+
1535
const env = jasmine.getEnv();
1636
env.beforeEach(() => {
1737
global.mockClipboardCopy = jest.fn();

scripts/jest/jest-cli.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,13 @@ const argv = yargs
9797
requiresArg: true,
9898
type: 'string',
9999
},
100+
compactConsole: {
101+
alias: 'c',
102+
describe: 'Compact console output (hide file locations).',
103+
requiresArg: false,
104+
type: 'boolean',
105+
default: false,
106+
},
100107
}).argv;
101108

102109
function logError(message) {
@@ -159,6 +166,11 @@ function validateOptions() {
159166
logError('DevTool tests require --build.');
160167
success = false;
161168
}
169+
} else {
170+
if (argv.compactConsole) {
171+
logError('Only DevTool tests support compactConsole flag.');
172+
success = false;
173+
}
162174
}
163175

164176
if (isWWWConfig()) {
@@ -284,6 +296,10 @@ function getEnvars() {
284296
RELEASE_CHANNEL: argv.releaseChannel.match(/modern|experimental/)
285297
? 'experimental'
286298
: '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,
287303
};
288304

289305
if (argv.prod) {
@@ -306,7 +322,9 @@ function main() {
306322
console.log(chalk.red(`\nPlease run: \`${argv.deprecated}\` instead.\n`));
307323
return;
308324
}
325+
309326
validateOptions();
327+
310328
const args = getCommandArgs();
311329
const envars = getEnvars();
312330
const env = Object.entries(envars).map(([k, v]) => `${k}=${v}`);
@@ -334,6 +352,7 @@ function main() {
334352
stdio: 'inherit',
335353
env: {...envars, ...process.env},
336354
});
355+
337356
// Ensure we close our process when we get a failure case.
338357
jest.on('close', code => {
339358
// Forward the exit code from the Jest process.

0 commit comments

Comments
 (0)