Skip to content

Commit

Permalink
Generalise package main resolving issue (#5968)
Browse files Browse the repository at this point in the history
* Generalise package main resolving issue

Resolves #5967

* Update changelog with info about #5968

* Fix linting errors
  • Loading branch information
mtlewis authored and cpojer committed Apr 12, 2018
1 parent 9e1f642 commit 36bfaf1
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@
([#5914](https://github.com/facebook/jest/pull/5914))
* `[jest-regex-util]` Fix handling regex symbols in tests path on Windows
([#5941](https://github.com/facebook/jest/pull/5941))
* `[jest-resolve]` Generalise test for package main entries equivalent to ".".
([#5968)](https://github.com/facebook/jest/pull/5968)

### Chore & Maintenance

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* 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.
*/

module.exports = 'test';
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "mock-module-alt",
"main": "./"
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,20 @@
'use strict';

jest.mock('mock-module');
jest.mock('mock-module-alt');
jest.mock('mock-jsx-module');

it('should resolve entry as index.js when package main is "."', () => {
const mockModule = require('mock-module');
expect(mockModule).toEqual('test');
});

it('should resolve entry as index with other configured module file extention when package main is "."', () => {
it('should resolve entry as index.js when package main is "./"', () => {
const mockModule = require('mock-module-alt');
expect(mockModule).toEqual('test');
});

it('should resolve entry as index with other configured module file extension when package main is "."', () => {
const mockJsxModule = require('mock-jsx-module');
expect(mockJsxModule).toEqual('test jsx');
});
6 changes: 5 additions & 1 deletion packages/jest-resolve/src/default_resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ function resolveSync(target: Path, options: ResolverOptions): Path {
pkgmain = JSON.parse(body).main;
} catch (e) {}

if (pkgmain && pkgmain !== '.') {
if (pkgmain && !isCurrentDirectory(pkgmain)) {
const resolveTarget = path.resolve(name, pkgmain);
const result = tryResolve(resolveTarget);
if (result) {
Expand Down Expand Up @@ -175,3 +175,7 @@ function isDirectory(dir: Path): boolean {

return result;
}

function isCurrentDirectory(testPath: Path): boolean {
return path.resolve('.') === path.resolve(testPath);
}

0 comments on commit 36bfaf1

Please sign in to comment.