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

Fix NODE_PATH resolving for relative paths #3616

Merged
merged 7 commits into from
May 25, 2017
Merged
Show file tree
Hide file tree
Changes from 3 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
19 changes: 19 additions & 0 deletions integration_tests/__tests__/node_path-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @emails oncall+jsinfra
*/
'use strict';

const runJest = require('../runJest');

test('supports NODE_PATH', () => {
const result = runJest('node_path', [], {
nodePath: ['../node_path/src'],
});
expect(result.status).toBe(0);
});
12 changes: 12 additions & 0 deletions integration_tests/node_path/__tests__/node_path-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
'use strict';

test('can require by absolute path', () => {
expect(require('path/file.js')).toBe(42);
});
8 changes: 8 additions & 0 deletions integration_tests/node_path/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"jest": {
"testEnvironment": "node",
"transform": {
"^.+\\.jsx?$": "../../packages/babel-jest"
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

babel-jest is the default transform, so it's not necessary to include it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I only added because it didn't reproduce otherwise. I'm not sure why?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because "babel-jest" would use the version from npm, this is fine.

}
}
10 changes: 10 additions & 0 deletions integration_tests/node_path/src/path/file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
'use strict';

module.exports = 42;
11 changes: 10 additions & 1 deletion integration_tests/runJest.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const JEST_PATH = path.resolve(__dirname, '../packages/jest-cli/bin/jest.js');

type RunJestOptions = {
skipPkgJsonCheck?: boolean, // don't complain if can't find package.json
nodePath?: string,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

abc, please move this up.

};

// return the result of the spawned process:
Expand Down Expand Up @@ -45,7 +46,15 @@ function runJest(
);
}

const result = spawnSync(JEST_PATH, args || [], {cwd: dir});
const env = options.nodePath
? Object.assign({}, process.env, {
NODE_PATH: options.nodePath,
})
: process.env;
const result = spawnSync(JEST_PATH, args || [], {
cwd: dir,
env,
});

result.stdout = result.stdout && result.stdout.toString();
result.stderr = result.stderr && result.stderr.toString();
Expand Down
6 changes: 5 additions & 1 deletion packages/jest-resolve/src/__tests__/resolve-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

'use strict';

const fs = require('fs');
const path = require('path');
const ModuleMap = require('jest-haste-map').ModuleMap;
const Resolver = require('../');
Expand Down Expand Up @@ -41,8 +42,11 @@ describe('isCoreModule', () => {

describe('findNodeModule', () => {
it('is possible to override the default resolver', () => {
const resolvedCwd = fs.realpathSync(process.cwd());
const nodePaths = process.env.NODE_PATH
? process.env.NODE_PATH.split(path.delimiter)
? process.env.NODE_PATH
.split(path.delimiter)
.map(p => path.resolve(resolvedCwd, p))
: null;

jest.mock('../__mocks__/userResolver');
Expand Down
8 changes: 7 additions & 1 deletion packages/jest-resolve/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import type {Path} from 'types/Config';
import type {ModuleMap} from 'types/HasteMap';

const fs = require('fs');
const path = require('path');
const nodeModulesPaths = require('resolve/lib/node-modules-paths');
const isBuiltinModule = require('is-builtin-module');
Expand Down Expand Up @@ -49,8 +50,13 @@ export type ResolveModuleConfig = {|

const NATIVE_PLATFORM = 'native';

// We might be inside a symlink.
const resolvedCwd = fs.realpathSync(process.cwd());
const nodePaths = process.env.NODE_PATH
? process.env.NODE_PATH.split(path.delimiter)
? process.env.NODE_PATH
.split(path.delimiter)
// The resolver expects absolute paths.
.map(p => path.resolve(resolvedCwd, p))
: null;

class Resolver {
Expand Down