The president has avoided a strong defense of pro-democracy protesters, but Congress has thrown the issue into his lap.

WASHINGTON — President Trump has spent months delicately sidestepping Hong Kong’s escalating battle between pro-democracy demonstrators and security services enforcing China’s authoritarian government line.

But on Wednesday, Congress put Mr. Trump on the spot, sending him tough legislation that would impose sanctions on Chinese officials for cracking down on the protesters and could end Hong Kong’s favored economic relationship with the United States.

The measure lands on the president’s desk at a sensitive moment. His trade talks with Beijing are becalmed, and a Dec. 15 deadline for Mr. Trump to decide whether to renew major tariffs on Chinese goods is approaching. China’s government has responded with fury to the measure and demanded that it not become law.

The bill, titled the Hong Kong Human Rights and Democracy Act, is a test of Mr. Trump’s commitment to the United States’ historical mission of promoting human rights and democracy abroad. Members of both parties have urged the president to speak out more forcefully on behalf of demonstrators resisting what they call Beijing’s tightening grip over the semiautonomous island territory, to little avail.

“He’s largely tried to ignore it so far,” said Laura Rosenberger, a top National Security Council aide for China affairs in the Obama White House.

“This is really going to be a test. In general his approach to the China challenge has been very focused on the trade aspects of the relationship,” added Ms. Rosenberger, a senior fellow at the German Marshall Fund of the United States.

The White House declined to comment on whether Mr. Trump would sign the measure, which passed the Senate unanimously and the House with only one lawmaker opposed, creating a solidly veto-proof majority. Presidents have 10 days to sign approved legislation, but that clock is suspended when Congress is adjourned, as it will be next week for the Thanksgiving holiday.

Speaking on CNBC on Thursday, the measure’s Senate sponsor, Marco Rubio, Republican of Florida, said that he expected it to win Mr. Trump’s approval.

“My understanding is they will sign it,” he said.

Mr. Trump could still invoke presidential authorities to avoid or delay enforcing some of the bill’s provisions, according to people familiar with the measure, potentially blunting its impact in the near term as well as criticism from Beijing.

“If the U.S. continues to make the wrong moves, China will be taking strong countermeasures for sure,” a spokesman for China’s Foreign Ministry, Geng Shuang, told reporters in Beijing on Thursday.

Without naming Mr. Trump, he urged the United States to “stop its wrongdoing before it’s too late, prevent this act from becoming law” and “immediately stop interfering in Hong Kong affairs and China’s internal affairs.”

China often issues angry criticisms of Washington with little follow up. And Chinese negotiators have also pushed to isolate the trade talks from a range of security-related matters, including the Trump administration’s crackdown on the Chinese telecom firm Huawei and the American sale of fighter jets to Taiwan.

“What has been surprising to me throughout the trade negotiations has been the degree of China’s willingness to compartmentalize issues,” said Evan S. Medeiros, another top China aide in the Obama White House.

“Obviously the Chinese don’t want the president to sign it. But are they going to be willing to blow up the trade deal to do so?” added Mr. Medeiros, now a professor at Georgetown University’s Walsh School of Foreign Service. “I don’t think they will, in large part because they understand he has a lot of discretion with this particular bill.”

In June, Mr. Trump told President Xi Jinping of China that he would not publicly back the protesters as long as trade talks were progressing. Administration officials have said that they have some reservations about striking a trade deal with China at a time when violence in Hong Kong could worsen, but that it is not the main impediment to reaching an agreement.

Congress acted on the measure, which had been stalled, after the riot police challenged student activists on campuses for the first time this month. Mr. Trump has said he hopes the crisis “works out for everybody,” and has suggested that the matter is an internal affair for China to resolve.

But leaders in both parties have pressed Mr. Trump to offer more support for the protesters.

“I would encourage this president, who has seen Chinese behavior for what it is with a clarity that others have lacked, not to shy away from speaking out on Hong Kong himself,” Senator Mitch McConnell of Kentucky, the majority leader, said on Wednesday. “The world should hear from him directly that the United States stands with these brave men and women.”

Senator Charles Schumer, the Democratic leader, denounced the president’s position, asking on Twitter this month, “Why is President Trump giving the Chinese Communist Party a free hand?”

While Mr. Trump has said little about Hong Kong to provoke Beijing, other administration officials have used stronger words. “We stand with you. We are inspired by you. We urge you to stay on the path of nonviolent protest,” Vice President Mike Pence said in an address last month.

The measure requires the State Department to certify annually whether Hong Kong’s government is maintaining democratic and civil society freedoms, such as judicial independence and freedom of the press, that were promised by China’s government when Britain handed over its former colony to Beijing in 1997. If Hong Kong fails that test, it would lose a preferred trading status that plays a major role in its economy.

It would also require sanctions on Chinese officials deemed responsible for arbitrary detention, torture, or forced confession of demonstrators and other political dissidents.

But the bill could be another impediment to a so-called Phase 1 trade deal whose fate is increasingly unclear.

Michael Pillsbury, a China expert at the conservative Hudson Institute with close ties to the Trump administration, said that Mr. Trump could blunt the measure’s impact on the trade talks by delaying his signature until after such a deal is reached.

Mr. Trump announced last month that he had reached a trade agreement with China that would strengthen intellectual property protections, lock in extensive purchases of American farm goods and forestall some of the tariff increases he had planned for later this year.

The agreement would help ease tensions in a trade war that has dragged on for more than a year and harmed businesses and consumers on both sides of the Pacific. But in recent weeks, the countries have sparred over the pact’s provisions, with China pushing for the removal of more of the tariffs that Mr. Trump has placed on $360 billion of its products.

The cancellation of a summit in Chile where Mr. Trump and Mr. Xi were expected to sign the agreement also threw plans for the pact’s completion into disarray.

Speaking to reporters on Wednesday as he toured an Apple plant in Austin, Texas, Mr. Trump said he had resisted a deal because China was not offering enough concessions.

“I haven’t wanted to do it yet because I don’t think they’re stepping up to the level that I want,” he said.

Chinese officials have asked their American counterparts to travel to China for another round of negotiations. But Mr. Trump’s advisers have been reluctant to do so, insisting that China is offering too little in the form of concessions to warrant the level of tariff relief it is requesting, one person briefed on their plans said.