From a124c3e4f47c46019c5d3eada5f16154700ea2b6 Mon Sep 17 00:00:00 2001 From: Jakob Ackermann Date: Wed, 21 Oct 2020 11:47:56 +0200 Subject: [PATCH] Merge pull request #3278 from overleaf/as-prop-type-test-fail Make prop-type errors fail the frontend tests GitOrigin-RevId: 4676239be8bfc541397740bdbe4044751e088c82 --- services/web/test/frontend/prop-types.test.js | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 services/web/test/frontend/prop-types.test.js diff --git a/services/web/test/frontend/prop-types.test.js b/services/web/test/frontend/prop-types.test.js new file mode 100644 index 0000000000..6c41bc8727 --- /dev/null +++ b/services/web/test/frontend/prop-types.test.js @@ -0,0 +1,28 @@ +/** + * Make tests fail when prop-types are incorrect. + * + * React's prop-types library logs an console.error when the types are + * incorrect. Since this error is only in the console, tests that fail the type- + * check will still pass. + * + * To ensure that type-checking is tested, monkey-patch the global console.error + * to fail the tests when prop-types errors. + */ +const originalConsoleError = global.console.error +before(function() { + global.console.error = (message, ...args) => { + // Ensure we still log the error + originalConsoleError(message, ...args) + + // Check if the error is from prop-types + if (/Failed prop type/.test(message)) { + // Throw an error, causing the test to fail + throw new Error(message) + } + } +}) + +// Restore the original method +after(function() { + global.console.error = originalConsoleError +})