MCPcopy
hub / github.com/facebook/react / expectTestToFail

Function expectTestToFail

scripts/jest/setupTests.js:165–217  ·  view source on GitHub ↗
(callback, errorToThrowIfTestSucceeds)

Source from the content-addressed store, hash-verified

163 }
164
165 const expectTestToFail = async (callback, errorToThrowIfTestSucceeds) => {
166 if (callback.length > 0) {
167 throw Error(
168 'Gated test helpers do not support the `done` callback. Return a ' +
169 'promise instead.'
170 );
171 }
172
173 // Install a global error event handler. We treat global error events as
174 // test failures, same as Jest's default behavior.
175 //
176 // Becaused we installed our own error event handler, Jest will not report a
177 // test failure. Conceptually it's as if we wrapped the entire test event in
178 // a try-catch.
179 let didError = false;
180 const errorEventHandler = () => {
181 didError = true;
182 };
183 // eslint-disable-next-line no-restricted-globals
184 if (typeof addEventListener === 'function') {
185 // eslint-disable-next-line no-restricted-globals
186 addEventListener('error', errorEventHandler);
187 }
188
189 try {
190 const maybePromise = callback();
191 if (
192 maybePromise !== undefined &&
193 maybePromise !== null &&
194 typeof maybePromise.then === 'function'
195 ) {
196 await maybePromise;
197 }
198 // Flush unexpected console calls inside the test itself, instead of in
199 // `afterEach` like we normally do. `afterEach` is too late because if it
200 // throws, we won't have captured it.
201 assertConsoleLogsCleared();
202 } catch (testError) {
203 didError = true;
204 }
205 resetAllUnexpectedConsoleCalls();
206 // eslint-disable-next-line no-restricted-globals
207 if (typeof removeEventListener === 'function') {
208 // eslint-disable-next-line no-restricted-globals
209 removeEventListener('error', errorEventHandler);
210 }
211
212 if (!didError) {
213 // The test did not error like we expected it to. Report this to Jest as
214 // a failure.
215 throw errorToThrowIfTestSucceeds;
216 }
217 };
218
219 const coerceGateConditionToFunction = gateFnOrString => {
220 return typeof gateFnOrString === 'string'

Callers 1

setupTests.jsFile · 0.70

Calls 4

assertConsoleLogsClearedFunction · 0.85
callbackFunction · 0.50
removeEventListenerFunction · 0.50

Tested by

no test coverage detected