-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
require.resolve with options.paths + require.resolve.paths (#6471)
- Loading branch information
Showing
13 changed files
with
236 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* 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 {resolve} = require('path'); | ||
|
||
const runJest = require('../runJest'); | ||
const {writeFiles, cleanup} = require('../Utils'); | ||
|
||
const workdirNodeModules = resolve( | ||
__dirname, | ||
'..', | ||
'resolve-with-paths', | ||
'node_modules', | ||
); | ||
|
||
beforeAll(() => { | ||
writeFiles(resolve(workdirNodeModules, 'mod'), { | ||
'index.js': 'module.exports = 42;', | ||
}); | ||
}); | ||
|
||
afterAll(() => { | ||
cleanup(workdirNodeModules); | ||
}); | ||
|
||
test('require.resolve with paths', () => { | ||
const {status} = runJest('resolve-with-paths'); | ||
expect(status).toBe(0); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"jest": { | ||
"testEnvironment": "node" | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
e2e/resolve-with-paths/__tests__/resolve-with-paths.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/** | ||
* 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('finds a module relative to one of the given paths', () => { | ||
expect(require.resolve('./mod.js', {paths: ['../dir']})).toEqual( | ||
resolve(__dirname, '..', 'dir', 'mod.js') | ||
); | ||
}); | ||
|
||
test('finds a module without a leading "./" relative to one of the given paths', () => { | ||
expect(require.resolve('mod.js', {paths: ['../dir']})).toEqual( | ||
resolve(__dirname, '..', 'dir', 'mod.js') | ||
); | ||
}); | ||
|
||
test('finds a node_module above one of the given paths', () => { | ||
expect(require.resolve('mod', {paths: ['../dir']})).toEqual( | ||
resolve(__dirname, '..', 'node_modules', 'mod', 'index.js') | ||
); | ||
}); | ||
|
||
test('finds a native node module when paths are given', () => { | ||
expect(require.resolve('fs', {paths: ['../dir']})).toEqual('fs'); | ||
}); | ||
|
||
test('throws an error if the module cannot be found from given paths', () => { | ||
expect(() => require.resolve('./mod.js', {paths: ['..']})).toThrowError( | ||
"Cannot resolve module './mod.js' from paths ['..'] from " | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = 'mod'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"jest": { | ||
"testEnvironment": "node" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters