Virginia Governor-elect Ralph Northam greets supporters at an election night rally on Nov. 7 in Fairfax. Northam defeated Republican candidate Ed Gillespie in a race that broke spending records. (Win McNamee/Getty Images)

Virginia gubernatorial contenders spent nearly $65 million this year as part of the most expensive governor race in the commonwealth's recent history, according to an analysis by the nonpartisan Virginia Public Access Project.

Democrat Ralph Northam, the lieutenant governor, spent nearly $36 million on his successful campaign, while his Republican rival Ed Gillespie poured in $29 million. Libertarian Cliff Hyra spent about $76,000.

As the first competitive gubernatorial contest this year, the race was widely viewed as a test of politics in the era of President Trump — and a hint of things to come in the 2018 midterms.

Both national parties poured money into Virginia, which does not cap campaign contributions.

Outgoing Gov. Terry McAuliffe, a prolific Democratic fundraiser before he sought office, still holds the record as the biggest-spending gubernatorial contender after deploying $39 million in his 2013 race.

The VPAP data, with fundraising numbers adjusted for inflation, looks only at spending during the election year. The group’s analysis followed the release of new campaign finance records on Thursday listing candidate donations and expenses between Oct. 27 and Nov. 30.

Those records show Gillespie outraised and outspent Northam by roughly $2 million in the final stretch of the campaign. But it wasn’t enough to save the longtime GOP operative and former chairman of the Republican National Committee from a surge of voters who showed up at the ballot box to vote Democratic and deliver a 9-point win for Northam.

Further down ballot, Republican Jill Vogel ran the most expensive lieutenant governor campaign in recent history, spending $4.3 million. While she outperformed the rest of the statewide GOP ticket, she lost to Democrat Justin Fairfax by 5.5 points. He spent roughly $4 million.

Attorney General Mark Herring (D) and his Republican challenger John Adams spent more than $9 million each in a race that far surpassed their predecessors’ spending. Much of that money came from national Republican and Democratic attorneys general groups, which this year ended an unofficial truce to stay out of races involving incumbents.

Democrats also posted stunning gains in the state legislature, unseating a dozen Republican incumbents as part of a 15-seat gain in the House of Delegates.

Nine out of 12 Republicans who lost their seats were outspent, according to VPAP.

The most notable exception was House Minority Whip Jackson Miller (R-Prince William), who spent far more than his opponent Lee Carter and still fell. Miller had enough money left in his campaign account that he transferred $180,000 to a political action committee after the election ended.

Democrat Dawn Adams spent just over $150,000 in her narrow upset victory over Del. G. Manoli Loupassi (R-Richmond), the lowest of any new members elected to the House. Adams, a nurse practitioner who would be the first open lesbian to serve in the Virginia legislature, won by a margin of 336 votes out of more than 39,000 votes cast. Loupassi is seeking a recount.

Money might have saved Republicans who narrowly won in three other close races that are heading to recounts.

Del. Tim Hugo (R-Fairfax) outspent Democrat Donte Tanner by nearly $200,000 this year and won by 106 votes. Del. David Yancey (R-Newport News) and his Democratic challenger Shelly Simonds were neck-and-neck in spending, as they were in the final outcome in which Yancey hung onto his seat with just 10 votes.

And in the Fredericksburg-area district held by retiring Speaker William J. Howell, Republican Bob Thomas spent $228,000 to Democrat Joshua Cole's $74,000. Thomas squeaked to victory by 82 votes, while Democrats are seeking both a recount and a new election after more than 100 voters cast ballots with the wrong districts.