diff --git a/CHANGELOG.md b/CHANGELOG.md index e07e38a4ac6a..33808e0697bd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ * `[jest-message-util]` Always remove node internals from stacktraces ([#4695](https://github.com/facebook/jest/pull/4695)) * `[jest-resolve]` changes method of determining builtin modules to include missing builtins ([#4740](https://github.com/facebook/jest/pull/4740)) * `[pretty-format]` Prevent error in pretty-format for window in jsdom test env ([#4750](https://github.com/facebook/jest/pull/4750)) +* `[jest-resolve]` Preserve module identity for symlinks ([#4761](https://github.com/facebook/jest/pull/4761)) ### Features * `[jest-environment-*]` [**BREAKING**] Add Async Test Environment APIs, dispose is now teardown ([#4506](https://github.com/facebook/jest/pull/4506)) diff --git a/integration_tests/resolve/__tests__/resolve.test.js b/integration_tests/resolve/__tests__/resolve.test.js index 7e66c5d06c8e..41a840c2d56d 100644 --- a/integration_tests/resolve/__tests__/resolve.test.js +++ b/integration_tests/resolve/__tests__/resolve.test.js @@ -56,3 +56,9 @@ test('should resolve filename.json', () => { expect(testRequire('../test4')).not.toThrow(); expect(platform.extension).toBe('json'); }); + +test('should preserve identity for symlinks', () => { + expect(require('../../../packages/jest-resolve')).toBe( + require('jest-resolve') + ); +}); diff --git a/packages/jest-resolve/src/default_resolver.js b/packages/jest-resolve/src/default_resolver.js index 56c4fc539a38..324777bec194 100644 --- a/packages/jest-resolve/src/default_resolver.js +++ b/packages/jest-resolve/src/default_resolver.js @@ -94,6 +94,11 @@ function resolveSync(target: Path, options: ResolverOptions): Path { if (isDirectory(dir)) { result = resolveAsFile(name) || resolveAsDirectory(name); } + if (result) { + // Dereference symlinks to ensure we don't create a separate + // module instance depending on how it was referenced. + result = fs.realpathSync(result); + } return result; }