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

Generalise package main resolving issue #5968

Merged
merged 3 commits into from
Apr 12, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
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
8 changes: 4 additions & 4 deletions docs/ExpectAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,8 @@ describe('not.stringContaining', () => {

### `expect.not.stringMatching(string | regexp)`

`expect.not.stringMatching(string | regexp)` matches the received string that does not
match the expected regexp.
`expect.not.stringMatching(string | regexp)` matches the received string that
does not match the expected regexp.

It is the inverse of `expect.stringMatching`.

Expand Down Expand Up @@ -384,8 +384,8 @@ exact expected string.

### `expect.stringMatching(string | regexp)`

`expect.stringMatching(string | regexp)` matches the received string that matches the
expected regexp.
`expect.stringMatching(string | regexp)` matches the received string that
matches the expected regexp.

You can use it instead of a literal value:

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);
}