Check out a free preview of the full Polyglot Programming: TypeScript, Go, & Rust course

The "Testing" Lesson is part of the full, Polyglot Programming: TypeScript, Go, & Rust course featured in this preview video. Here's what you'd learn in this lesson:

ThePrimeagen briefly walks through the unit testing organization and example code for TypeScript, Go, and Rust. Unit testing with Rust happens in the file and can test private interfaces.


Transcript from the "Testing" Lesson

>> We're gonna go over unit testing, and so unit testing sucks in JavaScript. As you know, you have to create these folders, then you have to download libraries, and then you have to knit the config. And if the config doesn't play well with your build system, you cry and configuration for a couple hours.

And eventually, you get a unit testing system up and running and it's fantastic, right? Go has the exact same thing except for its built in, right? So it has a testing already built in. The testing kind of sucks. We are gonna only rely on ghost testing library today.

Cuz I'm just gonna use the most canonical items cuz there are assertion libraries forego that do make testing a lot nicer. So I can just like assert on stuff as opposed to doing this. Where I'm like, hey, I expected it to equal 7. If it doesn't have to explicitly say the error right here, it doesn't give you that nice printing out and you kinda have to individually test things.

Which kinda sucks, right? If that's not exactly what you want. Rust of course, is significantly better. You test in the file. So you don't create a separate file for testing. And you do have to be careful about one thing with rust which is you gain the ability to test private interfaces, right?

And so that is always really painful when you can, the more you test private interfaces what happens? Your code changes, your testing underneath the hood. And then now when you make a change to your code, you have like way more stuff you need to change because more tests break.

So rust though, I think is one of the better ones for testing also provides more ability to make what I'd say is difficult decisions [LAUGH] with your life. So be careful about that. Rust test of code effectively looks like this. This is called a procedural macro or proc macro.

And it does something magical to your code. You don't know what it does, doesn't matter what it does. And then you just say hey, this thing is the test. Does something magical to your code. Doesn't matter. If you can just say assert equal, and it will break if it doesn't equal.

There's even a plugin or library you can download that overrides assert equals, and it will do like the full just style print out. If you have two objects, it'll say which properties aren't matching each other and you'll get that red, green and all that. So very, very fantastic.

I would again, we're not gonna use any additional libraries. We're just gonna use what is available. TypeScript being the only one that you can't do that with because there is no testing available with TypeScript. And you're going to forget everything I said, right? I just said a whole bunch of stuff.

We're all gonna forget it. So let's start building stuff, right?

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now