Skip to content

Commit

Permalink
feat(runtime): support require.resolve.paths
Browse files Browse the repository at this point in the history
  • Loading branch information
jeysal committed Jul 1, 2018
1 parent 7475747 commit 4991024
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

### Features

- `[jest-runtime]` Support `require.resolve.paths`.
- `[jest-runtime]` Support `paths` option for `require.resolve`.
- `[jest-snapshot]` Introduce `toMatchInlineSnapshot` and `toThrowErrorMatchingInlineSnapshot` matchers ([#6380](https://github.com/facebook/jest/pull/6380))

Expand Down
16 changes: 16 additions & 0 deletions e2e/__tests__/resolve-get-paths.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* 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.
*
* @flow
*/
'use strict';

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

test('require.resolve.paths', () => {
const {status} = runJest('resolve-get-paths');
expect(status).toBe(0);
});
24 changes: 24 additions & 0 deletions e2e/resolve-get-paths/__tests__/resolve-get-paths.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* 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.
*/
'use strict';

import {resolve} from 'path';

test('returns the resolve path for a relative path', () => {
expect(require.resolve.paths('./mod.js')).toEqual([resolve(__dirname)]);
});

test('returns the resolve paths for a node_module', () => {
expect(require.resolve.paths('mod').slice(0, 2)).toEqual([
resolve(__dirname, 'node_modules'),
resolve(__dirname, '..', 'node_modules'),
]);
});

test('returns null for a native node module', () => {
expect(require.resolve.paths('fs')).toBeNull();
});
5 changes: 5 additions & 0 deletions e2e/resolve-get-paths/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"jest": {
"testEnvironment": "node"
}
}
23 changes: 23 additions & 0 deletions packages/jest-runtime/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,27 @@ class Runtime {
return this._resolveModule(from, moduleName);
}

_requireResolvePaths(from: Path, moduleName?: string) {
if (moduleName == null) {
throw new Error(
'The first argument to require.resolve.paths must be a string. Received null or undefined.',
);
}
if (!moduleName.length) {
throw new Error(
'The first argument to require.resolve.paths must not be the empty string.',
);
}

if (moduleName[0] === '.') {
return [path.resolve(from, '..')];
}
if (this._resolver.isCoreModule(moduleName)) {
return null;
}
return this._resolver.getModulePaths(path.resolve(from, '..'));
}

_execModule(
localModule: Module,
options: ?InternalModuleOptions,
Expand Down Expand Up @@ -758,6 +779,8 @@ class Runtime {
moduleRequire.requireMock = this.requireMock.bind(this, from.filename);
moduleRequire.resolve = (moduleName, options) =>
this._requireResolve(from.filename, moduleName, options);
moduleRequire.resolve.paths = moduleName =>
this._requireResolvePaths(from.filename, moduleName);
Object.defineProperty(
moduleRequire,
'main',
Expand Down

0 comments on commit 4991024

Please sign in to comment.