Repository: facebook/react
File: scripts/jest/devtools/setupEnv.js (JavaScript)

Code before:
  }
};

global._test_ignore_for_react_version = (testName, callback) => {
  test.skip(testName, callback);
};

// Most of our tests call jest.resetModules in a beforeEach and the
// re-require all the React modules. However, the JSX runtime is injected by
// the compiler, so those bindings don't get updated. This causes warnings
// logged by the JSX runtime to not have a component stack, because component
// stack relies on the the secret internals object that lives on the React
// module, which because of the resetModules call is longer the same one.
//
// To workaround this issue, we use a proxy that re-requires the latest
// JSX Runtime from the require cache on every function invocation.
//
// Longer term we should migrate all our tests away from using require() and
// resetModules, and use import syntax instead so this kind of thing doesn't
// happen.
jest.mock('react/jsx-dev-runtime', () => {
  return new Proxy(
    {},
    {
      get(_, prop) {
        return jest.requireActual('react/jsx-dev-runtime')[prop];
      },
    }
  );
});

// TODO: We shouldn't need to do this in the production runtime, but until
// we remove string refs they also depend on the shared state object. Remove
// once we remove string refs.
jest.mock('react/jsx-runtime', () => {
  return new Proxy(
    {},
    {
      get(_, prop) {
        return jest.requireActual('react/jsx-runtime')[prop];
      },
    }
  );
});


Review comment (bug):
Shouldn't we proxy the functions exported by the runtime? the jsx bindings may not reevaluated after a reset still? I mean i must be working presumably since your tests are getting stacks but I wonder if this has edge cases that trapping the calls themselves wouldn't have?

Code after:
  }
};

global._test_ignore_for_react_version = (testName, callback) => {
  test.skip(testName, callback);
};

// Most of our tests call jest.resetModules in a beforeEach and the
// re-require all the React modules. However, the JSX runtime is injected by
// the compiler, so those bindings don't get updated. This causes warnings
// logged by the JSX runtime to not have a component stack, because component
// stack relies on the the secret internals object that lives on the React
// module, which because of the resetModules call is longer the same one.
//
// To workaround this issue, we use a proxy that re-requires the latest
// JSX Runtime from the require cache on every function invocation.
//
// Longer term we should migrate all our tests away from using require() and
// resetModules, and use import syntax instead so this kind of thing doesn't
// happen.
lazyRequireFunctionExports('react/jsx-dev-runtime');

// TODO: We shouldn't need to do this in the production runtime, but until
// we remove string refs they also depend on the shared state object. Remove
// once we remove string refs.
lazyRequireFunctionExports('react/jsx-runtime');

function lazyRequireFunctionExports(moduleName) {
  jest.mock(moduleName, () => {
    return new Proxy(jest.requireActual(moduleName), {
      get(originalModule, prop) {
        // If this export is a function, return a wrapper function that lazily
        // requires the implementation from the current module cache.
        if (typeof originalModule[prop] === 'function') {
          return function () {
            return jest.requireActual(moduleName)[prop].apply(this, arguments);
          };
        } else {
          return originalModule[prop];
        }
      },
    });
  });
}