index.js 944 Bytes
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41

let pnp;

try {
  pnp = require(`pnpapi`);
} catch (error) {
  // not in PnP; not a problem
}

let defaultResolver;

function requireDefaultResolver() {
  if (!defaultResolver) {
    try {
      defaultResolver = require(`jest-resolve/build/defaultResolver`).default;
    } catch (error) {
      defaultResolver = require(`jest-resolve/build/default_resolver`).default;
    }
  }

  return defaultResolver;
}

module.exports = (request, options) => {
  const {basedir, defaultResolver, extensions} = options;

  if (pnp) {
    const resolution = pnp.resolveRequest(request, `${basedir}/`, {extensions});

    // When the request is a native module, Jest expects to get the string back unmodified, but pnp returns null instead.
    if (resolution === null)
      return request;

    return resolution;
  } else {
    if (!defaultResolver)
      defaultResolver = requireDefaultResolver();

    return defaultResolver(request, options);
  }
};