Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve dynamic dependencies correctly when a mapping exists #9303

Merged
merged 9 commits into from
Dec 28, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@

### Fixes

- `[jest-resolve-dependencies]` Handle dynamic dependencies correctly even when using module maps ([#9303](https://github.com/facebook/jest/pull/9303))
MrJohz marked this conversation as resolved.
Show resolved Hide resolved
- `[expect]` Display `expectedDiff` more carefully in `toBeCloseTo` ([#8389](https://github.com/facebook/jest/pull/8389))
- `[expect]` Avoid incorrect difference for subset when `toMatchObject` fails ([#9005](https://github.com/facebook/jest/pull/9005))
- `[expect]` Consider all RegExp flags for equality ([#9167](https://github.com/facebook/jest/pull/9167))
Expand Down
17 changes: 17 additions & 0 deletions e2e/__tests__/watchDynamicRequires.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
MrJohz marked this conversation as resolved.
Show resolved Hide resolved
* 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 * as path from 'path';
import {json as runWithJson} from '../runJest';

const dir = path.resolve(__dirname, '../watch-dynamic-requires');

test('successfully transpiles async', () => {
const {json} = runWithJson(dir, ['--findRelatedTests', 'dynamicRequire.js']);
expect(json.success).toBe(true);
expect(json.numTotalTests).toBe(2);
});
16 changes: 16 additions & 0 deletions e2e/watch-dynamic-requires/__tests__/dynamicRequire.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* 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.
*/

test('loading a file with a dynamic local require should work', () => {
const {withStandardResolution} = require('../dynamicRequire');
expect(withStandardResolution()).toBe(1);
});

test('loading a file with a dynamic require and custom resolve should work', () => {
const {withCustomResolution} = require('../dynamicRequire');
expect(withCustomResolution()).toBe(1);
});
13 changes: 13 additions & 0 deletions e2e/watch-dynamic-requires/dynamicRequire.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* 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.
*/

const dynamicModuleName = 'source';

module.exports.withStandardResolution = () =>
require(`./${dynamicModuleName}.js`);
module.exports.withCustomResolution = () =>
require(`$asdf/${dynamicModuleName}.js`);
7 changes: 7 additions & 0 deletions e2e/watch-dynamic-requires/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"jest": {
"moduleNameMapper": {
"^\\$asdf/(.*)$": "<rootDir>/$1"
}
}
}
8 changes: 8 additions & 0 deletions e2e/watch-dynamic-requires/source.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* 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.
*/

module.exports = 1;
8 changes: 6 additions & 2 deletions packages/jest-resolve-dependencies/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,12 @@ class DependencyResolver {
dependency,
options,
);
} catch (e) {
resolvedDependency = this._resolver.getMockModule(file, dependency);
} catch (e1) {
MrJohz marked this conversation as resolved.
Show resolved Hide resolved
try {
resolvedDependency = this._resolver.getMockModule(file, dependency);
} catch (e2) {
resolvedDependency = null;
MrJohz marked this conversation as resolved.
Show resolved Hide resolved
}
}

if (resolvedDependency) {
Expand Down