18d307f52SEvan Baconconst resolveFrom = require(require.resolve('resolve-from'));
28d307f52SEvan Bacon
3*e1bb5bdfSKudo Chienconst silent = (fromDirectory: string, request: string) => {
48d307f52SEvan Bacon  const fs = require('fs');
58d307f52SEvan Bacon  const path = require('path');
68d307f52SEvan Bacon  try {
78d307f52SEvan Bacon    fromDirectory = fs.realpathSync(fromDirectory);
8*e1bb5bdfSKudo Chien  } catch (error: any) {
98d307f52SEvan Bacon    if (error.code === 'ENOENT') {
108d307f52SEvan Bacon      fromDirectory = path.resolve(fromDirectory);
118d307f52SEvan Bacon    } else {
128d307f52SEvan Bacon      return;
138d307f52SEvan Bacon    }
148d307f52SEvan Bacon  }
158d307f52SEvan Bacon
168d307f52SEvan Bacon  let outputPath = path.join(fromDirectory, 'node_modules', request);
178d307f52SEvan Bacon  if (fs.existsSync(outputPath)) {
188d307f52SEvan Bacon    return outputPath;
198d307f52SEvan Bacon  }
208d307f52SEvan Bacon  if (!path.extname(outputPath)) {
218d307f52SEvan Bacon    outputPath += '.js';
228d307f52SEvan Bacon  }
238d307f52SEvan Bacon  if (fs.existsSync(outputPath)) {
248d307f52SEvan Bacon    return outputPath;
258d307f52SEvan Bacon  }
268d307f52SEvan Bacon  outputPath = path.join(fromDirectory, request);
278d307f52SEvan Bacon  if (fs.existsSync(outputPath)) {
288d307f52SEvan Bacon    return outputPath;
298d307f52SEvan Bacon  }
308d307f52SEvan Bacon};
318d307f52SEvan Bacon
328d307f52SEvan Baconmodule.exports = jest.fn((fromDirectory, request) => {
338d307f52SEvan Bacon  const path = silent(fromDirectory, request);
348d307f52SEvan Bacon  if (!path) {
358d307f52SEvan Bacon    return resolveFrom(fromDirectory, request);
368d307f52SEvan Bacon  }
378d307f52SEvan Bacon  return path;
388d307f52SEvan Bacon});
398d307f52SEvan Bacon
408d307f52SEvan Baconmodule.exports.silent = jest.fn(silent);
41