Repository: nodejs/node
File: test/cctest/test_environment.cc (C++)

Code before:

  node::ModuleData entry_point;
  // Test dynamic import('node:process') in ESM entry point.
  std::string source =
      "export const importedPromise = import('node:process');\n";
  entry_point.set_source(source);
  entry_point.set_format(node::ModuleFormat::kModule);
  entry_point.set_resource_name("embedded:dynamic-import.mjs");

  v8::Local<v8::Value> result =
      node::LoadEnvironment(*env, &entry_point).ToLocalChecked();

  // The ESM entry point returns the module namespace object.
  EXPECT_TRUE(result->IsObject());
  v8::Local<v8::Value> imported_promise =
      result.As<v8::Object>()
          ->Get(isolate_->GetCurrentContext(),
                v8::String::NewFromUtf8Literal(isolate_, "importedPromise"))
          .ToLocalChecked();
  EXPECT_TRUE(imported_promise->IsPromise());

  v8::Local<v8::Context> context = isolate_->GetCurrentContext();
  // Finish the await.
  context->GetMicrotaskQueue()->PerformCheckpoint(isolate_);

  v8::Local<v8::Value> imported_value =
      imported_promise.As<v8::Promise>()->Result();
  EXPECT_TRUE(imported_value->IsObject());
  v8::Local<v8::Value> dynamic_process =
      imported_value.As<v8::Object>()
          ->Get(context, v8::String::NewFromUtf8Literal(isolate_, "default"))
          .ToLocalChecked();
  EXPECT_TRUE(dynamic_process->IsObject());

  v8::Local<v8::Value> global_process_value =
      context->Global()
          ->Get(context, v8::String::NewFromUtf8Literal(isolate_, "process"))
          .ToLocalChecked();
  EXPECT_TRUE(global_process_value->IsObject());
  EXPECT_TRUE(dynamic_process->StrictEquals(global_process_value));
}

TEST_F(EnvironmentTest, LoadEnvironmentWithESModuleImportMeta) {
  const v8::HandleScope handle_scope(isolate_);
  const Argv argv;
  Env env{handle_scope, argv};

  node::ModuleData entry_point;
  // Test import.meta properties in ESM entry point.
  std::string source = "export const url = import.meta.url;\n"
                       "export const main = import.meta.main;\n"

Review comment (suggestion):
```suggestion
  EXPECT_EQ(result.As<v8::Promise>()->State(), v8::Promise::kFulfilled);
  v8::Local<v8::Value> imported_value =
```

Code after:
    "  globalThis.importedNonBuiltinStack = err.stack)\n";
void CheckDynamicImportResult(v8::Isolate* isolate,
                              v8::Local<v8::Value> result) {
  v8::Local<v8::Context> context = isolate->GetCurrentContext();
  // Check the result of import('node:process').
  EXPECT_TRUE(result->IsObject());
  v8::Local<v8::Value> imported_process =
      result.As<v8::Object>()
          ->Get(context,
                v8::String::NewFromUtf8Literal(isolate, "importedProcess"))
          .ToLocalChecked();
  EXPECT_TRUE(imported_process->IsPromise());
  EXPECT_EQ(imported_process.As<v8::Promise>()->State(),
            v8::Promise::kFulfilled);
  v8::Local<v8::Value> imported_value =
      imported_process.As<v8::Promise>()->Result();
  EXPECT_TRUE(imported_value->IsObject());
  v8::Local<v8::Value> dynamic_process =
      imported_value.As<v8::Object>()
          ->Get(context, v8::String::NewFromUtf8Literal(isolate, "default"))
          .ToLocalChecked();
  EXPECT_TRUE(dynamic_process->IsObject());

  v8::Local<v8::Value> global_process_value =
      context->Global()
          ->Get(context, v8::String::NewFromUtf8Literal(isolate, "process"))
          .ToLocalChecked();
  EXPECT_TRUE(global_process_value->IsObject());
  EXPECT_TRUE(dynamic_process->StrictEquals(global_process_value));

  // Check the result of import('./non-existent.mjs')
  v8::Local<v8::Value> imported_non_builtin =
      result.As<v8::Object>()
          ->Get(context,
                v8::String::NewFromUtf8Literal(isolate, "importedNonBuiltin"))
          .ToLocalChecked();
  EXPECT_TRUE(imported_non_builtin->IsPromise());
  EXPECT_EQ(imported_non_builtin.As<v8::Promise>()->State(),
            v8::Promise::kRejected);

  // Check the error message
  v8::Local<v8::Value> imported_non_builtin_stack =
      context->Global()
          ->Get(context,
                v8::String::NewFromUtf8Literal(isolate,
                                               "importedNonBuiltinStack"))
          .ToLocalChecked();
  EXPECT_TRUE(imported_non_builtin_stack->IsString());
  node::Utf8Value error_str(isolate, imported_non_builtin_stack);
  // Error stack should include "ERR_UNKNOWN_BUILTIN_MODULE"
  EXPECT_NE(error_str.ToString().find("ERR_UNKNOWN_BUILTIN_MODULE"),