Current section: End-to-End Mocking 4 exercises

Handling Emails in Node.js using File System

Loading solution


00:00 Like I said, this exercise is pretty darn simple. So if we go to our mock resend, then we're already parsing the email and mocking out the email because we had to do that earlier for our development stuff. And you'll find that if you develop your software in such

00:17 a way that you can run offline, then adding automated testing will be that much easier. And so what we have here is this is where we're logging things to the terminal. But we also need to write it to disk. And so Kelly already put this together for us.

00:36 We get the dir name where we're currently located. We get the path join of that dir name. We're going to go up a directory. And then here's our fixtures. And then here's our email directory. And then we ensure that email directory exists so that it's ready to start writing these emails there.

00:53 And all we have to do is write the JSON. So we're using FS extra. That module is kind of nice. And we're going to write the JSON to that path. And the file name is going to be the address that the email was sent to, dot JSON. And then the contents, of course, will be that email.

01:13 And that's it. So Marty the money bag kind of cheated a little bit. But what's more important for you to take out of this is more like the process of this process writes to disk and this process reads from it. More of that than anything else. So I don't feel too bad about holding your hand too much on this one.

01:32 Because you're not here to learn how to write files to disk. You're here to learn how to test. So that's why we gave you that piece. OK, so with that in place now, if I hit play, then we should get an email written to disk. Now, the test isn't going to work yet, because we still have to read that email.

01:51 But if we look over here, right here, that email is 0v underscore. So we should find in our email directory, there it is, 0v underscore yada yada. And so this is what the email said. And from this, we can find the code.

02:09 It's going to be in here somewhere, right around here. So there's the code in there. And we pull that up, and we can continue on our merry way with this process. That's, like I said, pretty quick and painless. Now, the next part is to actually read this email in our test.

02:29 So let's move on.