Skip to content

Commit 1b7e044

Browse files
rickhanloniiAndyPengc12
authored andcommitted
[tests] Disallow unasserted console.log (facebook#28708)
Followup from facebook#28693 and facebook#28680. In CI, we fail the test for any unasserted console.log. In DEV, we don't fail, but you can still use the matchers and we'll assert on them.
1 parent 481f8ff commit 1b7e044

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

scripts/jest/matchers/__tests__/toWarnDev-test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,3 +416,20 @@ describe('toWarnDev', () => {
416416
});
417417
}
418418
});
419+
420+
describe('toLogDev', () => {
421+
it('does not fail if warnings do not include a stack', () => {
422+
expect(() => {
423+
if (__DEV__) {
424+
console.log('Hello');
425+
}
426+
}).toLogDev('Hello');
427+
expect(() => {
428+
if (__DEV__) {
429+
console.log('Hello');
430+
console.log('Good day');
431+
console.log('Bye');
432+
}
433+
}).toLogDev(['Hello', 'Good day', 'Bye']);
434+
});
435+
});

scripts/jest/matchers/toWarnDev.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,9 @@ const createMatcherFor = (consoleMethod, matcherName) =>
178178
};
179179
}
180180

181-
if (typeof withoutStack === 'number') {
181+
if (consoleMethod === 'log') {
182+
// We don't expect any console.log calls to have a stack.
183+
} else if (typeof withoutStack === 'number') {
182184
// We're expecting a particular number of warnings without stacks.
183185
if (withoutStack !== warningsWithoutComponentStack.length) {
184186
return {
@@ -309,4 +311,5 @@ const createMatcherFor = (consoleMethod, matcherName) =>
309311
module.exports = {
310312
toWarnDev: createMatcherFor('warn', 'toWarnDev'),
311313
toErrorDev: createMatcherFor('error', 'toErrorDev'),
314+
toLogDev: createMatcherFor('log', 'toLogDev'),
312315
};

scripts/jest/setupTests.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,28 +116,37 @@ if (process.env.REACT_CLASS_EQUIVALENCE_TEST) {
116116
.join('\n')}`
117117
);
118118

119+
const type = methodName === 'log' ? 'log' : 'warning';
119120
const message =
120121
`Expected test not to call ${chalk.bold(
121122
`console.${methodName}()`
122123
)}.\n\n` +
123-
'If the warning is expected, test for it explicitly by:\n' +
124+
`If the ${type} is expected, test for it explicitly by:\n` +
124125
`1. Using the ${chalk.bold('.' + expectedMatcher + '()')} ` +
125126
`matcher, or...\n` +
126127
`2. Mock it out using ${chalk.bold(
127128
'spyOnDev'
128129
)}(console, '${methodName}') or ${chalk.bold(
129130
'spyOnProd'
130-
)}(console, '${methodName}'), and test that the warning occurs.`;
131+
)}(console, '${methodName}'), and test that the ${type} occurs.`;
131132

132133
throw new Error(`${message}\n\n${messages.join('\n\n')}`);
133134
}
134135
};
135136

136137
const unexpectedErrorCallStacks = [];
137138
const unexpectedWarnCallStacks = [];
139+
const unexpectedLogCallStacks = [];
138140

139141
const errorMethod = patchConsoleMethod('error', unexpectedErrorCallStacks);
140142
const warnMethod = patchConsoleMethod('warn', unexpectedWarnCallStacks);
143+
let logMethod;
144+
145+
// Only assert console.log isn't called in CI so you can debug tests in DEV.
146+
// The matchers will still work in DEV, so you can assert locally.
147+
if (process.env.CI) {
148+
logMethod = patchConsoleMethod('log', unexpectedLogCallStacks);
149+
}
141150

142151
const flushAllUnexpectedConsoleCalls = () => {
143152
flushUnexpectedConsoleCalls(
@@ -152,13 +161,25 @@ if (process.env.REACT_CLASS_EQUIVALENCE_TEST) {
152161
'toWarnDev',
153162
unexpectedWarnCallStacks
154163
);
164+
if (logMethod) {
165+
flushUnexpectedConsoleCalls(
166+
logMethod,
167+
'log',
168+
'toLogDev',
169+
unexpectedLogCallStacks
170+
);
171+
unexpectedLogCallStacks.length = 0;
172+
}
155173
unexpectedErrorCallStacks.length = 0;
156174
unexpectedWarnCallStacks.length = 0;
157175
};
158176

159177
const resetAllUnexpectedConsoleCalls = () => {
160178
unexpectedErrorCallStacks.length = 0;
161179
unexpectedWarnCallStacks.length = 0;
180+
if (logMethod) {
181+
unexpectedLogCallStacks.length = 0;
182+
}
162183
};
163184

164185
beforeEach(resetAllUnexpectedConsoleCalls);

0 commit comments

Comments
 (0)