I wouldn't say "congrats" unless I was trying to sound like a cool teenager but didn't realize that cool teenagers stopped saying that decades ago. I might say "congratulations" (it doesn't take much longer to say than the shorter version), "well done," "way to go" or any of several other things. It would depend in part on the test that they passed. A pop quiz in a 10th grade history course isn't as significant as a doctoral qualifying exam.
(As an aside, if in another context where the two people aren't really physically close to each other and are moving in opposite directions, say like in a crowded hallway, the second person could just give a "thumbs up" sign of approval to the test-passer, just like this icon represented here: )
Nothing is being spoken, so issues of formal or non-formal wouldn't apply.