diff --git a/packages/jest-runtime/src/__mocks__/resourceQueryResolver.js b/packages/jest-runtime/src/__mocks__/resourceQueryResolver.js new file mode 100644 index 000000000000..cfaf6fd46271 --- /dev/null +++ b/packages/jest-runtime/src/__mocks__/resourceQueryResolver.js @@ -0,0 +1,14 @@ +/** + * 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. + */ + +'use strict'; + +module.exports = function userResolver(path, options) { + const defaultResolver = require('../__tests__/defaultResolver.js'); + const [clearPath, query] = path.split('?'); + return defaultResolver(clearPath, options) + (query ? '?' + query : ''); +}; diff --git a/packages/jest-runtime/src/__tests__/runtime_require_module.test.js b/packages/jest-runtime/src/__tests__/runtime_require_module.test.js index 4f678f9daac2..b220ad5417b2 100644 --- a/packages/jest-runtime/src/__tests__/runtime_require_module.test.js +++ b/packages/jest-runtime/src/__tests__/runtime_require_module.test.js @@ -249,6 +249,47 @@ describe('Runtime requireModule', () => { expect(hastePackage.isHastePackage).toBe(true); })); + it('supports resolving the same path to multiple modules', () => + createRuntime(__filename, { + // using the default resolver as a custom resolver + resolver: require.resolve('../__mocks__/resourceQueryResolver.js'), + }).then(runtime => { + const moduleNoQuery1 = runtime.requireModule( + runtime.__mockRootPath, + './moduleForResourceQuery.js', + ); + const moduleNoQuery2 = runtime.requireModule( + runtime.__mockRootPath, + './moduleForResourceQuery.js', + ); + expect(moduleNoQuery1.name).toBe('moduleForResourceQuery'); + expect(moduleNoQuery1).toBe(moduleNoQuery2); + + const moduleWithQueryA = runtime.requireModule( + runtime.__mockRootPath, + './moduleForResourceQuery.js?a', + ); + const moduleWithQueryB = runtime.requireModule( + runtime.__mockRootPath, + './moduleForResourceQuery.js?b', + ); + expect(moduleWithQueryA.name).toBe('moduleForResourceQuery'); + expect(moduleWithQueryB.name).toBe('moduleForResourceQuery'); + expect(moduleWithQueryA).not.toBe(moduleWithQueryB); + + const moduleWithSameQuery1 = runtime.requireModule( + runtime.__mockRootPath, + './moduleForResourceQuery.js?sameQuery', + ); + const moduleWithSameQuery2 = runtime.requireModule( + runtime.__mockRootPath, + './moduleForResourceQuery.js?sameQuery', + ); + expect(moduleWithSameQuery1.name).toBe('moduleForResourceQuery'); + expect(moduleWithSameQuery2.name).toBe('moduleForResourceQuery'); + expect(moduleWithSameQuery1).toBe(moduleWithSameQuery2); + })); + it('resolves node modules properly when crawling node_modules', () => // While we are crawling a node module, we shouldn't put package.json // files of node modules to resolve to `package.json` but rather resolve diff --git a/packages/jest-runtime/src/__tests__/test_root/moduleForResourceQuery.js b/packages/jest-runtime/src/__tests__/test_root/moduleForResourceQuery.js new file mode 100644 index 000000000000..ddc4688b8b2a --- /dev/null +++ b/packages/jest-runtime/src/__tests__/test_root/moduleForResourceQuery.js @@ -0,0 +1,11 @@ +/** + * 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. + */ + +// this object reference should be uniq per each module compilation +const newObject = {name: 'moduleForResourceQuery'}; + +module.exports = newObject;