Home / Function/ expectTestToFail() — react Function Reference

expectTestToFail() — react Function Reference

Architecture documentation for the expectTestToFail() function in setupTests.js from the react codebase.

Entity Profile

Dependency Diagram

graph TD
  fe71b168_00a5_4f64_7563_c24a6ba87598["expectTestToFail()"]
  01d850dc_3214_54c0_e858_c1194dd15ff0["setupTests.js"]
  fe71b168_00a5_4f64_7563_c24a6ba87598 -->|defined in| 01d850dc_3214_54c0_e858_c1194dd15ff0
  909084f5_b0b3_1893_708d_ff31a1d296c3["global()"]
  909084f5_b0b3_1893_708d_ff31a1d296c3 -->|calls| fe71b168_00a5_4f64_7563_c24a6ba87598
  style fe71b168_00a5_4f64_7563_c24a6ba87598 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

scripts/jest/setupTests.js lines 176–228

  const expectTestToFail = async (callback, errorToThrowIfTestSucceeds) => {
    if (callback.length > 0) {
      throw Error(
        'Gated test helpers do not support the `done` callback. Return a ' +
          'promise instead.'
      );
    }

    // Install a global error event handler. We treat global error events as
    // test failures, same as Jest's default behavior.
    //
    // Becaused we installed our own error event handler, Jest will not report a
    // test failure. Conceptually it's as if we wrapped the entire test event in
    // a try-catch.
    let didError = false;
    const errorEventHandler = () => {
      didError = true;
    };
    // eslint-disable-next-line no-restricted-globals
    if (typeof addEventListener === 'function') {
      // eslint-disable-next-line no-restricted-globals
      addEventListener('error', errorEventHandler);
    }

    try {
      const maybePromise = callback();
      if (
        maybePromise !== undefined &&
        maybePromise !== null &&
        typeof maybePromise.then === 'function'
      ) {
        await maybePromise;
      }
      // Flush unexpected console calls inside the test itself, instead of in
      // `afterEach` like we normally do. `afterEach` is too late because if it
      // throws, we won't have captured it.
      assertConsoleLogsCleared();
    } catch (testError) {
      didError = true;
    }
    resetAllUnexpectedConsoleCalls();
    // eslint-disable-next-line no-restricted-globals
    if (typeof removeEventListener === 'function') {
      // eslint-disable-next-line no-restricted-globals
      removeEventListener('error', errorEventHandler);
    }

    if (!didError) {
      // The test did not error like we expected it to. Report this to Jest as
      // a failure.
      throw errorToThrowIfTestSucceeds;
    }
  };

Domain

Subdomains

Called By

Frequently Asked Questions

What does expectTestToFail() do?
expectTestToFail() is a function in the react codebase, defined in scripts/jest/setupTests.js.
Where is expectTestToFail() defined?
expectTestToFail() is defined in scripts/jest/setupTests.js at line 176.
What calls expectTestToFail()?
expectTestToFail() is called by 1 function(s): global.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free