Sen. Jon Tester — perhaps the Democratic incumbent President Trump most dearly wanted to defeat — eked out a win for a third term. Even though Trump won Montana by 20 percentage points in 2016, Tester managed to defeat his Republican opponent, state auditor Matt Rosendale.

It’s a blow to Trump, who has been furious at Tester for months because of the senator’s role in the defeat of Trump’s scandal-plagued VA secretary nominee Ronny Jackson. Tester, the top Democrat on the Senate Veterans Affairs Committee, went public about allegations that Jackson drank on the job and improperly gave out medication — and Trump was eventually forced to withdraw the nomination.

Related 4 winners and 2 losers from the 2018 midterm elections

Though Tester was simply exercising his oversight role, Trump seems to have taken this as a personal humiliation (Jackson was his doctor in the White House). So he retaliated with vituperation against Tester that went above and beyond what he aimed at any other vulnerable Senate Democrat this year. “Tester should lose race in Montana. Very dishonest and sick!” he tweeted.

Yet Tester won anyway — showing the limits of the president’s influence, even in a very red state.

How Tester keeps winning in a conservative state

When Tester, Montana’s state Senate president, jumped in the 2006 race for US Senate, few gave him good odds of winning. An opponent in the Democratic primary was better-funded and better-known. And though the incumbent Republican senator was plagued with corruption scandals, George W. Bush had just beaten John Kerry by 20 points in Montana.

But Tester struck a chord. He was a farmer who couldn’t be effectively portrayed as an out-of-touch political elite, and he embraced plain-spoken, red-state economic populism. This helped him win the passionate support of both locals and national Democratic “netroots” activists. He cruised to victory in the primary easily.

In his first general election, though, he barely squeaked by with a mere 0.87 percent margin amid a Democratic wave in 2006. But that win proved crucial, since Democrats overall won the exact number of seats they needed for a Senate majority. Once in office, Tester effectively maintained his brand of political pragmatism and independence, and won reelection by 4 percentage points in 2012, even though Republican presidential nominee Mitt Romney won Montana by 14.

As Tester’s bid for a third term approached, Trump inadvertently helped him out a bit, nominating Republican Rep. Ryan Zinke, his likely opponent, to be secretary of the interior. Republicans were then left with a somewhat unimpressive field of candidates, from which Rosendale narrowly emerged.

Even during the primary, Rosendale was blasted as a carpetbagger (he moved to the state from Maryland 16 years ago but held on to property there until recently). Tester, who is well-known in the state as a farmer, also questioned Rosendale’s presentation of himself as a “rancher,” pointing out that he didn’t actually own any cattle.

Indeed, Republicans were bearish on Rosendale’s chances all year — he never led in a single poll tracked by RealClearPolitics. The conventional wisdom was that Tester was one of the safest of the deep red state Senate Democrats on the ballot (though some believed the race was tightening in the closing weeks). So Tester has pulled it off yet again — and added another six years to what’s been a surprisingly durable career in national politics.