11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ciconst common = require('../common'); 31cb0ef41Sopenharmony_ciconst tmpdir = require('../common/tmpdir'); 41cb0ef41Sopenharmony_ciconst assert = require('assert'); 51cb0ef41Sopenharmony_ciconst { spawnSync } = require('child_process'); 61cb0ef41Sopenharmony_ciconst fs = require('fs'); 71cb0ef41Sopenharmony_ciconst path = require('path'); 81cb0ef41Sopenharmony_ciconst { createRequire } = require('module'); 91cb0ef41Sopenharmony_ci 101cb0ef41Sopenharmony_cifor (const name in ['test', 'test/reporters']) { 111cb0ef41Sopenharmony_ci assert.throws( 121cb0ef41Sopenharmony_ci () => require(name), 131cb0ef41Sopenharmony_ci common.expectsError({ code: 'MODULE_NOT_FOUND' }), 141cb0ef41Sopenharmony_ci ); 151cb0ef41Sopenharmony_ci 161cb0ef41Sopenharmony_ci (async () => { 171cb0ef41Sopenharmony_ci await assert.rejects( 181cb0ef41Sopenharmony_ci async () => import(name), 191cb0ef41Sopenharmony_ci common.expectsError({ code: 'ERR_MODULE_NOT_FOUND' }), 201cb0ef41Sopenharmony_ci ); 211cb0ef41Sopenharmony_ci })().then(common.mustCall()); 221cb0ef41Sopenharmony_ci 231cb0ef41Sopenharmony_ci assert.throws( 241cb0ef41Sopenharmony_ci () => require.resolve(name), 251cb0ef41Sopenharmony_ci common.expectsError({ code: 'MODULE_NOT_FOUND' }), 261cb0ef41Sopenharmony_ci ); 271cb0ef41Sopenharmony_ci} 281cb0ef41Sopenharmony_ci 291cb0ef41Sopenharmony_ci// Verify that files in node_modules can be resolved. 301cb0ef41Sopenharmony_citmpdir.refresh(); 311cb0ef41Sopenharmony_ci 321cb0ef41Sopenharmony_ciconst packageRoot = path.join(tmpdir.path, 'node_modules', 'test'); 331cb0ef41Sopenharmony_ciconst reportersDir = path.join(tmpdir.path, 'node_modules', 'test', 'reporters'); 341cb0ef41Sopenharmony_ciconst indexFile = path.join(packageRoot, 'index.js'); 351cb0ef41Sopenharmony_ciconst reportersIndexFile = path.join(reportersDir, 'index.js'); 361cb0ef41Sopenharmony_ci 371cb0ef41Sopenharmony_cifs.mkdirSync(reportersDir, { recursive: true }); 381cb0ef41Sopenharmony_cifs.writeFileSync(indexFile, 'module.exports = { marker: 1 };'); 391cb0ef41Sopenharmony_cifs.writeFileSync(reportersIndexFile, 'module.exports = { marker: 1 };'); 401cb0ef41Sopenharmony_ci 411cb0ef41Sopenharmony_cifunction test(argv, expectedToFail = false) { 421cb0ef41Sopenharmony_ci const child = spawnSync(process.execPath, argv, { cwd: tmpdir.path }); 431cb0ef41Sopenharmony_ci if (expectedToFail) { 441cb0ef41Sopenharmony_ci assert.strictEqual(child.status, 1); 451cb0ef41Sopenharmony_ci assert.strictEqual(child.stdout.toString().trim(), ''); 461cb0ef41Sopenharmony_ci } else { 471cb0ef41Sopenharmony_ci assert.strictEqual(child.status, 0); 481cb0ef41Sopenharmony_ci assert.strictEqual(child.stdout.toString().trim(), '{ marker: 1 }'); 491cb0ef41Sopenharmony_ci } 501cb0ef41Sopenharmony_ci} 511cb0ef41Sopenharmony_ci 521cb0ef41Sopenharmony_citest(['-e', 'console.log(require("test"))']); 531cb0ef41Sopenharmony_citest(['-e', 'console.log(require("test/reporters"))']); 541cb0ef41Sopenharmony_citest(['-e', 'import("test").then(m=>console.log(m.default))']); 551cb0ef41Sopenharmony_citest(['-e', 'import("test/reporters").then(m=>console.log(m.default))'], true); 561cb0ef41Sopenharmony_citest(['--input-type=module', '-e', 'import test from "test";console.log(test)']); 571cb0ef41Sopenharmony_citest(['--input-type=module', '-e', 'import test from "test/reporters";console.log(test)'], true); 581cb0ef41Sopenharmony_citest(['--input-type=module', '-e', 'console.log((await import("test")).default)']); 591cb0ef41Sopenharmony_citest(['--input-type=module', '-e', 'console.log((await import("test/reporters")).default)'], true); 601cb0ef41Sopenharmony_ci 611cb0ef41Sopenharmony_ci{ 621cb0ef41Sopenharmony_ci const dummyFile = path.join(tmpdir.path, 'file.js'); 631cb0ef41Sopenharmony_ci const require = createRequire(dummyFile); 641cb0ef41Sopenharmony_ci assert.strictEqual(require.resolve('test'), indexFile); 651cb0ef41Sopenharmony_ci assert.strictEqual(require.resolve('test/reporters'), reportersIndexFile); 661cb0ef41Sopenharmony_ci} 67