Repository: jestjs/jest (TypeScript)

Issue:
[Bug]: `require.resolve` with empty `paths` option does not locate absolute paths
### Version

27.2.4

### Steps to reproduce

See repo README at https://github.com/ekelen/jest-require-resolve-empty-paths-array 

OR:

1. Clone reproduction repo linked above and change current directory to repo root.
2. Run `cp test.js /tmp && node -p "require.resolve('/tmp/test', { paths: [] } )"` (returns path of `tmp/test.js` at filesystem root).
3. Run `yarn install`.
4. Run `yarn test`.

### Expected behavior

No error should be thrown when `require.resolve` is called with a valid module at user's filesystem root and an empty `paths` array option. 

The output of `require.resolve("/tmp/test", { paths: ["/"] })` should match the output of `require.resolve("/tmp/test", { paths: [] })`.

### Actual behavior

If provided an empty paths array, `ModuleNotFoundError` is thrown: `Cannot resolve module '/tmp/test' from paths [''] from <repo_root>/test.js`.

### Additional context

Appears to be a parallel issue to https://github.com/facebook/jest/issues/9502.

### Environment

```shell
System:
    OS: macOS 11.5.2
    CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
  Binaries:
    Node: 16.8.0 - ~/.volta/tools/image/node/16.8.0/bin/node
    Yarn: 1.22.15 - ~/.volta/tools/image/yarn/1.22.15/bin/yarn
    npm: 7.21.0 - ~/.volta/tools/image/node/16.8.0/bin/npm
  npmPackages:
    jest: 27.2.4 => 27.2.4
```



Hints:

Patch:
diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts
--- a/packages/jest-runtime/src/index.ts
+++ b/packages/jest-runtime/src/index.ts
@@ -1242,28 +1242,39 @@ export default class Runtime {
       );
     }
 
-    const {paths} = options;
-
-    if (paths) {
-      for (const p of paths) {
-        const absolutePath = path.resolve(from, '..', p);
-        const module = this._resolver.resolveModuleFromDirIfExists(
-          absolutePath,
-          moduleName,
-          // required to also resolve files without leading './' directly in the path
-          {conditions: this.cjsConditions, paths: [absolutePath]},
-        );
-        if (module) {
-          return module;
-        }
+    if (path.isAbsolute(moduleName)) {
+      const module = this._resolver.resolveModuleFromDirIfExists(
+        moduleName,
+        moduleName,
+        {conditions: this.cjsConditions, paths: []},
+      );
+      if (module) {
+        return module;
       }
+    } else {
+      const {paths} = options;
+      if (paths) {
+        for (const p of paths) {
+          const absolutePath = path.resolve(from, '..', p);
+          const module = this._resolver.resolveModuleFromDirIfExists(
+            absolutePath,
+            moduleName,
+            // required to also resolve files without leading './' directly in the path
+            {conditions: this.cjsConditions, paths: [absolutePath]},
+          );
+          if (module) {
+            return module;
+          }
+        }
 
-      throw new Resolver.ModuleNotFoundError(
-        `Cannot resolve module '${moduleName}' from paths ['${paths.join(
-          "', '",
-        )}'] from ${from}`,
-      );
+        throw new Resolver.ModuleNotFoundError(
+          `Cannot resolve module '${moduleName}' from paths ['${paths.join(
+            "', '",
+          )}'] from ${from}`,
+        );
+      }
     }
+
     try {
       return this._resolveModule(from, moduleName, {
         conditions: this.cjsConditions,


Test patch:
diff --git a/packages/jest-runtime/src/__tests__/runtime_require_resolve.test.ts b/packages/jest-runtime/src/__tests__/runtime_require_resolve.test.ts
--- a/packages/jest-runtime/src/__tests__/runtime_require_resolve.test.ts
+++ b/packages/jest-runtime/src/__tests__/runtime_require_resolve.test.ts
@@ -6,6 +6,9 @@
  *
  */
 
+import os from 'os';
+import path from 'path';
+import {promises as fs} from 'graceful-fs';
 import type {Config} from '@jest/types';
 import type Runtime from '..';
 import {createOutsideJestVmPath} from '../helpers';
@@ -15,6 +18,9 @@ let createRuntime: (
   config?: Config.InitialOptions,
 ) => Promise<Runtime & {__mockRootPath: string}>;
 
+const getTmpDir = async () =>
+  await fs.mkdtemp(path.join(os.tmpdir(), 'jest-resolve-test-'));
+
 describe('Runtime require.resolve', () => {
   beforeEach(() => {
     createRuntime = require('createRuntime');
@@ -29,6 +35,47 @@ describe('Runtime require.resolve', () => {
     expect(resolved).toEqual(require.resolve('./test_root/resolve_self.js'));
   });
 
+  it('resolves an absolute module path', async () => {
+    const absoluteFilePath = path.join(await getTmpDir(), 'test.js');
+    await fs.writeFile(
+      absoluteFilePath,
+      'module.exports = require.resolve(__filename);',
+      'utf-8',
+    );
+
+    const runtime = await createRuntime(__filename);
+    const resolved = runtime.requireModule(
+      runtime.__mockRootPath,
+      absoluteFilePath,
+    );
+
+    expect(resolved).toEqual(require.resolve(absoluteFilePath));
+  });
+
+  it('required modules can resolve absolute module paths with no paths entries passed', async () => {
+    const tmpdir = await getTmpDir();
+    const entrypoint = path.join(tmpdir, 'test.js');
+    const target = path.join(tmpdir, 'target.js');
+
+    // we want to test the require.resolve implementation within a
+    // runtime-required module, so we need to create a module that then resolves
+    // an absolute path, so we need two files: the entrypoint, and an absolute
+    // target to require.
+    await fs.writeFile(
+      entrypoint,
+      `module.exports = require.resolve(${JSON.stringify(
+        target,
+      )}, {paths: []});`,
+      'utf-8',
+    );
+
+    await fs.writeFile(target, `module.exports = {}`, 'utf-8');
+
+    const runtime = await createRuntime(__filename);
+    const resolved = runtime.requireModule(runtime.__mockRootPath, entrypoint);
+    expect(resolved).toEqual(require.resolve(target, {paths: []}));
+  });
+
   it('resolves a module path with moduleNameMapper', async () => {
     const runtime = await createRuntime(__filename, {
       moduleNameMapper: {