diff --git a/packages/jest-runtime/src/__mocks__/resourceQueryResolver.js b/packages/jest-runtime/src/__mocks__/resourceQueryResolver.js new file mode 100644 index 000000000000..2984b9e8250b --- /dev/null +++ b/packages/jest-runtime/src/__mocks__/resourceQueryResolver.js @@ -0,0 +1,15 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. 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 054f5c4a25f0..51930c4a097f 100644 --- a/packages/jest-runtime/src/__tests__/runtime_require_module.test.js +++ b/packages/jest-runtime/src/__tests__/runtime_require_module.test.js @@ -248,6 +248,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, + './moduleWithResourceQuery.js', + ); + const moduleNoQuery2 = runtime.requireModule( + runtime.__mockRootPath, + './moduleWithResourceQuery.js', + ); + expect(moduleNoQuery1.name).toBe('moduleWithoutResourceQuery'); + expect(moduleNoQuery1).toBe(moduleNoQuery2); + + const moduleWithQueryA = runtime.requireModule( + runtime.__mockRootPath, + './moduleWithResourceQuery.js?a', + ); + const moduleWithQueryB = runtime.requireModule( + runtime.__mockRootPath, + './moduleWithResourceQuery.js?b', + ); + expect(moduleWithQueryA.name).toBe('moduleWithoutResourceQuery'); + expect(moduleWithQueryB.name).toBe('moduleWithoutResourceQuery'); + expect(moduleWithQueryA).not.toBe(moduleWithQueryB); + + const moduleWithSameQuery1 = runtime.requireModule( + runtime.__mockRootPath, + './moduleWithResourceQuery.js?sameQuery', + ); + const moduleWithSameQuery2 = runtime.requireModule( + runtime.__mockRootPath, + './moduleWithResourceQuery.js?sameQuery', + ); + expect(moduleWithSameQuery1.name).toBe('moduleWithoutResourceQuery'); + expect(moduleWithSameQuery2.name).toBe('moduleWithoutResourceQuery'); + 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/moduleWithResourceQuery.js b/packages/jest-runtime/src/__tests__/test_root/moduleWithResourceQuery.js new file mode 100644 index 000000000000..6202fe8d1add --- /dev/null +++ b/packages/jest-runtime/src/__tests__/test_root/moduleWithResourceQuery.js @@ -0,0 +1,12 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. 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: 'moduleWithoutResourceQuery'}; + +module.exports = newObject;