Skip to content

Commit

Permalink
craft a test
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-leonov committed Mar 8, 2020
1 parent 1932269 commit 97d507d
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
14 changes: 14 additions & 0 deletions packages/jest-runtime/src/__mocks__/resourceQueryResolver.js
Original file line number Diff line number Diff line change
@@ -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 : '');
};
41 changes: 41 additions & 0 deletions packages/jest-runtime/src/__tests__/runtime_require_module.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 97d507d

Please sign in to comment.