Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Summary: This method can be used in a test to ensure that there is no unexpected console errors are logged. We should use `warning` / `invariant` in the Relay to inform developers about runtime exceptions/errors. Directly using `console.error` makes the output in the tests very hard to read. Reviewed By: josephsavona Differential Revision: D34276063 fbshipit-source-id: 9265b2c701706adde9bcfefddd8614ed9f4b6b26
- Loading branch information
1 parent
0cfdd54
commit 3738783
Showing
2 changed files
with
45 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @emails oncall+relay | ||
* @flow strict-local | ||
* @format | ||
*/ | ||
|
||
'use strict'; | ||
|
||
/* global jest, afterEach */ | ||
|
||
let installed = false; | ||
|
||
/** | ||
* Similar to disallowWarnings. | ||
* This method mocks the console.error and throws in the error is printed in the console. | ||
*/ | ||
function disallowConsoleErrors(): void { | ||
if (installed) { | ||
throw new Error('`disallowConsoleErrors` should be called at most once'); | ||
} | ||
installed = true; | ||
let errors = []; | ||
jest.spyOn(console, 'error').mockImplementation(message => { | ||
errors.push(`Unexpected \`console.error\`:\n${message}.`); | ||
}); | ||
afterEach(() => { | ||
if (errors.length > 0) { | ||
const message = errors.join('\n'); | ||
errors = []; | ||
throw new Error(message); | ||
} | ||
}); | ||
} | ||
|
||
module.exports = { | ||
disallowConsoleErrors, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters