Repository: jestjs/jest (TypeScript)

Issue:
[Bug]: issue with jest-runtime between 27.1.0 and 27.1.1 and jest.isolateModules
### Version

27.2.0

### Steps to reproduce

unable to link my repo, but can give an example that currently works. seems how mocks are applied changed between 27.1.0 and 27.1.1. assume related to this PR: https://github.com/facebook/jest/pull/11818

### Expected behavior

```javascript
const someModule = require('some-module');

jest.mock('some-module')

describe('test', () => {
  let app;
  beforeEach(() => {
    jest.isolateModules(() => {
      app = require('./app.js')
    })
  })

  test('expect module to have been called', () => {
    expect(someModule.function).toHaveBeenCalled();
  })
})
```
I would expect this to pass, which is currently does if I pass in a yarn resolution to downgrade `jest-runtime` to version `27.1.0`.

### Actual behavior

Using above example, the instance of `some-module` inside of the test and inside the required file appear to be different. Inside the file, the module is indeed mocked, but the expectation does not pass.

### Additional context

_No response_

### Environment

```shell
System:
    OS: macOS 11.5.2
    CPU: (12) x64 Intel(R) Core(TM) i9-8950HK CPU @ 2.90GHz
  Binaries:
    Node: 14.17.6 - /usr/local/bin/node
    Yarn: 1.22.11 - /usr/local/bin/yarn
    npm: 7.21.1 - /usr/local/bin/npm
  npmPackages:
    jest: 27.2.0 => 27.2.0
```



Hints:
have the same problem with ts-jest, here is a repro repo https://github.com/rburgst/ts-jest-27.1
Please put together a minimal repository showing the error (i.e. without `ts-jest` - I have zero clue what `mocked` is). Ideally without typescript etc either
Actually never mind, just ripping it out wasn't too bad - fix incoming 🙂 

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
@@ -861,12 +861,14 @@ export default class Runtime {
       {conditions: this.cjsConditions},
     );
 
-    const mockRegistry = this._isolatedMockRegistry || this._mockRegistry;
-
-    if (mockRegistry.get(moduleID)) {
-      return mockRegistry.get(moduleID);
+    if (this._isolatedMockRegistry?.has(moduleID)) {
+      return this._isolatedMockRegistry.get(moduleID);
+    } else if (this._mockRegistry.has(moduleID)) {
+      return this._mockRegistry.get(moduleID);
     }
 
+    const mockRegistry = this._isolatedMockRegistry || this._mockRegistry;
+
     if (this._mockFactories.has(moduleID)) {
       // has check above makes this ok
       const module = this._mockFactories.get(moduleID)!();


Test patch:
diff --git a/e2e/__tests__/isolateModules.test.ts b/e2e/__tests__/isolateModules.test.ts
new file mode 100644
--- /dev/null
+++ b/e2e/__tests__/isolateModules.test.ts
@@ -0,0 +1,55 @@
+/**
+ * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
+ *
+ * This source code is licensed under the MIT license found in the
+ * LICENSE file in the root directory of this source tree.
+ */
+
+import {tmpdir} from 'os';
+import * as path from 'path';
+import {cleanup, createEmptyPackage, writeFiles} from '../Utils';
+import runJest from '../runJest';
+
+const DIR = path.resolve(tmpdir(), 'isolate-modules.test');
+
+beforeEach(() => {
+  cleanup(DIR);
+  createEmptyPackage(DIR);
+});
+
+afterAll(() => cleanup(DIR));
+
+test('works with mocks', () => {
+  writeFiles(DIR, {
+    'config.js': `
+      module.exports.getBoolean = function getBoolean(variableName) {
+        return false;
+      }
+    `,
+    'read.js': `
+      const {getBoolean} = require('./config');
+
+      const value = getBoolean('foo');
+      console.log("was " + value);
+    `,
+    'test.js': `
+      jest.mock('./config');
+      const config = require('./config');
+
+      test('dummy test', () => {
+        const configGetMock = config.getBoolean.mockImplementation(() => {
+          return true;
+        });
+
+        jest.isolateModules(() => {
+          require("./read");
+        });
+
+        expect(configGetMock).toBeCalledTimes(1);
+      })
+    `,
+  });
+  const {exitCode} = runJest(DIR);
+
+  expect(exitCode).toBe(0);
+});