Gov. Bruce Rauner (R-Ill.) fended off a challenge from a conservative state lawmaker in Tuesday’s primary. (Rich Saal/AP)

Democratic Rep. Daniel Lipinski, a seven-term congressman often at odds with his party on social issues, narrowly defeated his liberal challenger in the Illinois primary Tuesday night in a test of the establishment’s strength against insurgents.

With 96 percent of the precincts reporting, fewer than 2,000 votes separated Lipinski from activist Marie Newman, a first-time candidate who described the incumbent as a “Trump Democrat.”

Lipinski addressed supporters at his campaign party before the race was called, telling them “I’m not coming up here to declare a victory,” he said, “but things are looking really good.”

Meanwhile Newman told her supporters Tuesday night that she “would like Mr. Lipinski to have a very painful evening, so we’re going to wait.”

“We have moved him on immigration, we have moved him on health care. ... So let’s be clear, the fight is not over and it is not done,” she added.

Newman attracted the support of national liberal groups, but Lipinski relied on the strength of the party’s turnout machine in Chicago and a familiar name. Bill Lipinski had held the seat for 22 years until his son succeeded him in 2005.

First-term Republican Gov. Bruce Rauner also faced a tough challenge but ultimately beat Jeanne Ives, a conservative state legislator who accused him of betraying his political base on abortion, gay rights, and spending.

“To those of you around the state who wanted to send me a message, let me be clear: I have heard you,” Rauner told supporters in his victory speech.

Rauner will face billionaire J.B. Pritzker, who secured the Democratic nomination, defeating Chris Kennedy, a member of the storied political family, and state Sen. Daniel Biss. Pritzker had spent more than $60 million to win the race.

The primaries for governor and Lipinski’s House seat pitted candidates who often compromised with the other party against challengers who demanded ideological purity, reflecting broader fights playing out in Washington and across the country.

Despite the numbers, Newman’s backers were defiant on Tuesday night, saying that they’d gotten behind her to send a message.

“We are literally fighting to protect our democracy,” said Marj Halperin, 62, a communications consultant, at Newman’s party on Chicago’s south side. “The greatest contribution Donald Trump has given to the country is energizing us to stand up.”

Lipinski, the policy chairman of the conservative Blue Dog Democrats, had built a moderate record in the House, opposing the Affordable Care Act because of his antiabortion stand. He also voted for religious freedom bills seen as making it easier for private businesses to discriminate against gay customers.

Local Democrats and labor unions consistently endorsed Lipinski, citing his votes against trade deals and ability to bring transportation money back to the district.

Lipinski has held off challengers before, but Newman hit the congressman with advertisements about his votes on health care and social issues.

“I know what’s in his heart, and it’s called hate,” Newman, told supporters last week. “This guy is dangerous. His views are dangerous.”

Newman, a first-time candidate who was the Illinois spokeswoman for the gun-control group Moms Demand Action, received endorsements from NARAL Pro-Choice America, MoveOn, Emily’s List and two of Lipinski’s more liberal Illinois colleagues, Reps. Jan Schakowsky (D) and Luis V. Gutiérrez (D).

Rep. Mark Pocan (D-Wis.), the co-chair of the Congressional Progressive Caucus, said he had endorsed Newman in part because of Lipinski’s votes for religious freedom bills.

“There’s a lot of districts where I’m backing people who are a lot more conservative than I am, because that’s the way we’re going to take them back,” Pocan said. “But when you have a district that looks as blue as this one in Illinois, I’d prefer that it be represented by someone who wants my husband and I to have full civil rights.”

Republicans have already written off the district. After no credible candidate filed to run, the only Republican on the ballot will be Art Jones, a neo-Nazi activist denounced by the party, making the Democratic nominee all but certain to serve in Congress.

But Blue Dog Democrats, conservatives who have shrunk to a small rump of their party, had chastised the Democratic Congressional Campaign Committee for staying neutral. Rep. Kurt Schrader (D-Ore.), the Blue Dogs’ chairman, said he was reassured when Pelosi endorsed Lipinski, and did not expect the close race to encourage more primary challenges.

“I’m not one of those people who discourages folks from running, but I’d hope that Democrats would try and channel their enthusiasms against other people, not fellow Democrats,” Schrader said. “I’d hope the long-term view is that we need to get to 218 in the House.”

No incumbent Democrat in Congress has lost a primary since 2014, when Massachusetts’s ­scandal-plagued Rep. John F. Tierney was defeated by Seth Moulton. No congressional Democrat has lost a primary seen as an ideological contest since 2008, when Maryland’s Donna F. Edwards ousted Rep. Albert R. Wynn.

Rauner’s near-miss in the primary could be a turning point for the unpopular political newcomer, who faced criticism for the tumult that has dominated his first term. Rauner kept a low profile over the weekend while Ives maintained an active campaign schedule.

The former private-equity executive provoked the conservative right in September when he signed a bill expanding abortion coverage for women on Medicaid. He also engaged in a long budget standoff with the state legislature that left Illinois $15 billion behind on its bills and triggered multiple downgrades that pushed the state’s credit rating almost to junk status.

Rauner had put $50 million of his own money into his campaign, much of it on ads that blamed Democratic House Speaker Mike Madigan for the state’s problems. But donor Richard Uihlein put $2.5  million behind Ives, a conservative legislator from Chicago’s western suburbs. Sensing an opening, the Democratic Governors Association paid for an ad branding Ives “too conservative,” in what was seen as a ploy to boost her among Republicans.

In Rogers Park, a lakeside city neighborhood, Sylvester Gunther, 59, said he was happy that Rauner was being challenged from all sides. The two-year budget stalemate did irreparable damage to the state, Gunther said, particularly among the most vulnerable, who experienced social services cuts.

The Republican “created a crisis and wants someone to blame. It’s time for someone to do something different because it’s hurt a lot of people,” he said.

Edith Thorne, 75, agreed. She cast her vote for Pritzker.

Rauner has “not done any good for our state,” she said. “By not passing a budget he really put us behind the eight ball.”

Pritzker and Rauner, both billionaire business executives, have spent $127 million combined on the campaign.

Pritzker, whom Democratic leaders saw early on as the strongest challenger to the wealthy Rauner, struggled after the Chicago Tribune released tapes of conversations between him and disgraced former governor Rod Blagojevich from an FBI wiretap.

Democrats also faced a crowded primary for attorney general, with state Sen. Kwame Raoul holding a lead over former governor Pat Quinn.

The party was also watching a scrum in the 6th Congressional District, which Hillary Clinton won by seven points in 2016 after Barack Obama lost it in 2012. Seven Democrats are battling to challenge Rep. Peter J. Roskam (R-Ill.) there, with scientist Kelly Mazeski leading in the early count. Democrat Betsy Dirksen Londrigan was projected to win in the race to challenge Rep. Rodney Davis (R-Ill.), in a central Illinois district evenly divided between the parties.

Mark Guarino in Chicago contributed to this report.