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

Code before:
          .ToLocalChecked();
  EXPECT_TRUE(dirname_value->IsString());
  node::Utf8Value dirname_str(isolate_, dirname_value);
  // Just check that dirname is a substring of exec_path
  EXPECT_NE(exec_path.find(dirname_str.ToStringView()), std::string::npos);
}

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

  node::ModuleData entry_point;
  // Test dynamic import('node:process') in CJS entry point.
  std::string source = "return import('node:process');\n";
  entry_point.set_source(source);
  entry_point.set_format(node::ModuleFormat::kCommonJS);
  entry_point.set_resource_name("/test-cjs-dynamic-import.js");

  v8::Local<v8::Value> result =
      node::LoadEnvironment(*env, &entry_point).ToLocalChecked();
  EXPECT_TRUE(result->IsPromise());
  v8::Local<v8::Context> context = isolate_->GetCurrentContext();
  // Finish the await.
  context->GetMicrotaskQueue()->PerformCheckpoint(isolate_);
  v8::Local<v8::Value> imported_value = result.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, LoadEnvironmentWithCallbackWithCommonJSModule) {
  const v8::HandleScope handle_scope(isolate_);
  const Argv argv;
  Env env{handle_scope, argv};

  v8::Local<v8::Context> context = isolate_->GetCurrentContext();

  v8::MaybeLocal<v8::Value> maybe = node::LoadEnvironment(
      *env,
      [&](const node::StartExecutionCallbackInfoWithModule& info)

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

Code after:
  // Finish the await.
  v8::Local<v8::Context> context = isolate_->GetCurrentContext();
  context->GetMicrotaskQueue()->PerformCheckpoint(isolate_);
  CheckDynamicImportResult(isolate_, result);
}

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"
                       "export const filename = import.meta.filename;\n"
                       "export const dirname = import.meta.dirname;\n";
  std::string exec_path = (*env)->exec_path();
  std::string url = node::url::FromFilePath(exec_path);
  entry_point.set_source(source);
  entry_point.set_format(node::ModuleFormat::kModule);
  entry_point.set_resource_name(url);

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

  EXPECT_TRUE(result->IsObject());
  v8::Local<v8::Context> context = isolate_->GetCurrentContext();

  // Check import.meta.url
  v8::Local<v8::Value> url_value =
      result.As<v8::Object>()
          ->Get(context, v8::String::NewFromUtf8Literal(isolate_, "url"))
          .ToLocalChecked();
  EXPECT_TRUE(url_value->IsString());
  node::Utf8Value url_str(isolate_, url_value);
  EXPECT_EQ(url_str.ToStringView(), url);

  // Check import.meta.main
  v8::Local<v8::Value> main_value =
      result.As<v8::Object>()
          ->Get(context, v8::String::NewFromUtf8Literal(isolate_, "main"))
          .ToLocalChecked();
  EXPECT_TRUE(main_value->IsBoolean());
  EXPECT_TRUE(main_value->BooleanValue(isolate_));

  // Check import.meta.filename
  v8::Local<v8::Value> filename_value =
      result.As<v8::Object>()
          ->Get(context, v8::String::NewFromUtf8Literal(isolate_, "filename"))
          .ToLocalChecked();