![]() ![]() Tip 3: Experiment with the humble debugger Rather than just having plain console logs, now you’ll have structured entries, that should be easier to make sense of, and more suited to the debugging style that you’re performing. Now you’ll see the output of all the three different console commands, and you can experiment with them to get a better feel for how they change the debugging experience. Save the file as app.js and run with the command: node app.js If you have ever tried to console log a native Node.js object and gotten a HUGE output, you’ll know how hard it is to read.īut instead, with console.dir and a depth property, you get the following: process ) In this options object, you can specify the depth of your console log. The first takes an object the second takes an object of options. The reason I’m giving this a shoutout is because it takes two arguments, the last of which is the important one. This is particularly useful for long or large objects with many properties, as it gives us more structure which can help with readability, like so: ┌─────────┬────────┐ Console.table does what you’d expect it to: it takes an array or an object and outputs an ASCII table. However in Node.js, since you only have the console to log to, each group is indented like so: I am a console log indented by a single level In the browser, this would nest all your logs together. Let me show you a couple of my favorites.īy running oup, you create a new collection of JavaScript console logs. If you’re not, it simply takes a string or an object and prints it. But what most developers don’t know is that the console object itself has many more useful methods that are often left underutilized. Most JavaScript developers are familiar with the console log. Tip 2: Explore alternative console methods This means you don’t have to have the app running separately, and it makes mocking dependencies such as database connections easy.Ĭombine your Jest unit tests with your SuperTest ones with Husky (a Git hook utility) pre-commit hook, and you can run all your unit and integration tests before your code even leaves your machine. SuperTest is particularly cool because you can pass it an Express app object directly. If you’re building APIs, SuperTest is a great testing library for API endpoints. But one of my absolute favorite features for Jest is how you can mock required dependencies simply. Not only is Jest super fast, but it comes with lots of goodies as well, such as the ability to create coverage reports by passing a coverage flag to your testing reports. ![]() If you want to get up and running with tests, I can highly recommend Jest as a unit testing library. map?” it is immeasurably helpful to see the creator’s thoughts encapsulated in test case descriptions and example code. When you’re rifling through code searching for a bug and stop to ask yourself, “Hold up, why are they using a. Tests are so useful during debugging because you can use them as documentation of your code’s behavior and APIs. The tests you skip writing today will short-change your future developers’ debugging abilities. ![]() Yet spending the past few years as a consultant developer, I’ve now seen so many teams hand-wave at test-driven development and opt instead to put themselves into a world of pain. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |