(CNN) Democratic Sen. Jon Tester will win a third term in the Senate, CNN projects, outlasting millions of outside spending and an intense focus on the race from President Donald Trump.

Tester was widely seen early in the cycle as a vulnerable Democrat due to the fact Trump won the state by 20 points just two years ago. But Tester's unique brand as a farmer from Big Sandy and personal connection to the state provided a contrast to Republican Matt Rosendale, whose ties to the state were constantly in question.

Tester, like other Democrats running in red states, routinely pledged throughout the campaign to work with Trump when needed, even running TV ads touting his work with Trump.

"Washington is a mess but that is not stopping me from getting bills signed into law by President Trump," Tester said in his first campaign ad

That relationship with Trump frayed, though, after Tester led the charge against Ronny L. Jackson, the former White House physician who Trump picked to lead the Department of Veterans Affairs. Tester, the top Democrat on the Senate committee that oversaw Jackson's confirmation, accused Jackson of handing out prescriptions "like candy" on foreign trips, accusations that eventually sunk his nomination.

Read More