11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ciconst common = require('../common'); 31cb0ef41Sopenharmony_ciconst assert = require('assert'); 41cb0ef41Sopenharmony_ciconst path = require('path'); 51cb0ef41Sopenharmony_ciconst nodeModules = path.join(__dirname, 'node_modules'); 61cb0ef41Sopenharmony_ciconst nestedNodeModules = path.join(__dirname, 'node_modules', 'node_modules'); 71cb0ef41Sopenharmony_ciconst nestedIndex = path.join(__dirname, 'nested-index'); 81cb0ef41Sopenharmony_ci 91cb0ef41Sopenharmony_ci// Test the default behavior. 101cb0ef41Sopenharmony_ciassert.strictEqual( 111cb0ef41Sopenharmony_ci require.resolve('bar'), 121cb0ef41Sopenharmony_ci path.join(nodeModules, 'bar.js') 131cb0ef41Sopenharmony_ci); 141cb0ef41Sopenharmony_ci 151cb0ef41Sopenharmony_ci// Verify that existing paths are removed. 161cb0ef41Sopenharmony_ciassert.throws(() => { 171cb0ef41Sopenharmony_ci require.resolve('bar', { paths: [] }) 181cb0ef41Sopenharmony_ci}, /^Error: Cannot find module 'bar'/); 191cb0ef41Sopenharmony_ci 201cb0ef41Sopenharmony_ci// Verify that resolution path can be overwritten. 211cb0ef41Sopenharmony_ci{ 221cb0ef41Sopenharmony_ci // three.js cannot be loaded from this file by default. 231cb0ef41Sopenharmony_ci assert.throws(() => { 241cb0ef41Sopenharmony_ci require.resolve('three') 251cb0ef41Sopenharmony_ci }, /^Error: Cannot find module 'three'/); 261cb0ef41Sopenharmony_ci 271cb0ef41Sopenharmony_ci // If the nested-index directory is provided as a resolve path, 'three' 281cb0ef41Sopenharmony_ci // cannot be found because nested-index is used as a starting point and not 291cb0ef41Sopenharmony_ci // a searched directory. 301cb0ef41Sopenharmony_ci assert.throws(() => { 311cb0ef41Sopenharmony_ci require.resolve('three', { paths: [nestedIndex] }) 321cb0ef41Sopenharmony_ci }, /^Error: Cannot find module 'three'/); 331cb0ef41Sopenharmony_ci 341cb0ef41Sopenharmony_ci // Resolution from nested index directory also checks node_modules. 351cb0ef41Sopenharmony_ci assert.strictEqual( 361cb0ef41Sopenharmony_ci require.resolve('bar', { paths: [nestedIndex] }), 371cb0ef41Sopenharmony_ci path.join(nodeModules, 'bar.js') 381cb0ef41Sopenharmony_ci ); 391cb0ef41Sopenharmony_ci} 401cb0ef41Sopenharmony_ci 411cb0ef41Sopenharmony_ci// Verify that the default paths can be used and modified. 421cb0ef41Sopenharmony_ci{ 431cb0ef41Sopenharmony_ci const paths = require.resolve.paths('bar'); 441cb0ef41Sopenharmony_ci 451cb0ef41Sopenharmony_ci assert.strictEqual(paths[0], nodeModules); 461cb0ef41Sopenharmony_ci assert.strictEqual( 471cb0ef41Sopenharmony_ci require.resolve('bar', { paths }), 481cb0ef41Sopenharmony_ci path.join(nodeModules, 'bar.js') 491cb0ef41Sopenharmony_ci ); 501cb0ef41Sopenharmony_ci 511cb0ef41Sopenharmony_ci paths.unshift(nestedNodeModules); 521cb0ef41Sopenharmony_ci assert.strictEqual( 531cb0ef41Sopenharmony_ci require.resolve('bar', { paths }), 541cb0ef41Sopenharmony_ci path.join(nodeModules, 'bar.js') 551cb0ef41Sopenharmony_ci ); 561cb0ef41Sopenharmony_ci} 571cb0ef41Sopenharmony_ci 581cb0ef41Sopenharmony_ci// Verify that relative request paths work properly. 591cb0ef41Sopenharmony_ci{ 601cb0ef41Sopenharmony_ci const searchIn = './' + path.relative(process.cwd(), nestedIndex); 611cb0ef41Sopenharmony_ci 621cb0ef41Sopenharmony_ci // Search in relative paths. 631cb0ef41Sopenharmony_ci assert.strictEqual( 641cb0ef41Sopenharmony_ci require.resolve('./three.js', { paths: [searchIn] }), 651cb0ef41Sopenharmony_ci path.join(nestedIndex, 'three.js') 661cb0ef41Sopenharmony_ci ); 671cb0ef41Sopenharmony_ci 681cb0ef41Sopenharmony_ci // Search in absolute paths. 691cb0ef41Sopenharmony_ci assert.strictEqual( 701cb0ef41Sopenharmony_ci require.resolve('./three.js', { paths: [nestedIndex] }), 711cb0ef41Sopenharmony_ci path.join(nestedIndex, 'three.js') 721cb0ef41Sopenharmony_ci ); 731cb0ef41Sopenharmony_ci 741cb0ef41Sopenharmony_ci // Repeat the same tests with Windows slashes in the request path. 751cb0ef41Sopenharmony_ci if (common.isWindows) { 761cb0ef41Sopenharmony_ci assert.strictEqual( 771cb0ef41Sopenharmony_ci require.resolve('.\\three.js', { paths: [searchIn] }), 781cb0ef41Sopenharmony_ci path.join(nestedIndex, 'three.js') 791cb0ef41Sopenharmony_ci ); 801cb0ef41Sopenharmony_ci 811cb0ef41Sopenharmony_ci assert.strictEqual( 821cb0ef41Sopenharmony_ci require.resolve('.\\three.js', { paths: [nestedIndex] }), 831cb0ef41Sopenharmony_ci path.join(nestedIndex, 'three.js') 841cb0ef41Sopenharmony_ci ); 851cb0ef41Sopenharmony_ci } 861cb0ef41Sopenharmony_ci} 871cb0ef41Sopenharmony_ci 881cb0ef41Sopenharmony_ci// Test paths option validation 891cb0ef41Sopenharmony_ciassert.throws(() => { 901cb0ef41Sopenharmony_ci require.resolve('.\\three.js', { paths: 'foo' }) 911cb0ef41Sopenharmony_ci}, { 921cb0ef41Sopenharmony_ci code: 'ERR_INVALID_ARG_VALUE', 931cb0ef41Sopenharmony_ci name: 'TypeError', 941cb0ef41Sopenharmony_ci}); 951cb0ef41Sopenharmony_ci 961cb0ef41Sopenharmony_ci// Verify that the default require.resolve() is used for empty options. 971cb0ef41Sopenharmony_ciassert.strictEqual( 981cb0ef41Sopenharmony_ci require.resolve('./printA.js', {}), 991cb0ef41Sopenharmony_ci require.resolve('./printA.js') 1001cb0ef41Sopenharmony_ci); 1011cb0ef41Sopenharmony_ci 1021cb0ef41Sopenharmony_ciassert.strictEqual( 1031cb0ef41Sopenharmony_ci require.resolve('no_index/'), 1041cb0ef41Sopenharmony_ci path.join(__dirname, 'node_modules', 'no_index', 'lib', 'index.js'), 1051cb0ef41Sopenharmony_ci) 106