Wigan moved back in to the top four with an exciting win over play-off chasing Wakefield.
The game's outcome wasn't confirmed until Harrison Hansen crashed over for the Warriors in the final minute.
Wakefield had threatened an unlikely late turnaround with tries in the final five minutes from Damien Blanch and Matt Blaymire reducing arrears to 32-30 with less than two minutes remaining.
Former Widnes winger Blanch gave the home side an early year but Thomas Leuluai cancelled that score out on the quarter hour.
Jay Pitts and Andy Coley then exchanged converted tries, with Pat Richards' penalty goal just before the break proving the difference between the sides, as Wigan went in 14-12 to the good.
The Wildcats hit the front at the start of the second half when Tony Martin crossed the whitewash, but the lead only lasted five minutes as Liam Colbon's try enabled Richards to edge the Warriors back in front.
The visitors then scored a further two tries through Richards and Gareth Hock to put them in what seemed to be a winning position at 32-18.
But a late flourish from the Wildcats kept the crowd on their toes until Hansen's four pointer confirmed the result at the death.
Wakefield 30 (12)
Tries: Blanch (6, 79), Pitts (26), Martin (47), Blaymire (75). Goals: Brough (7, 27, 48, 75, 80)
Wigan 38 (14)
Tries: Leulaui (15), Coley (30), Colbon (51), Richards (56), Hock (62), Hansen (80). Goals: Richards (16, 31, 38, 57, 61, 63, 80)
Referee: B Thaler