
Master Node.js unit testing by exploring callbacks, promises, test doubles (spies and stubs), routes, ES6 classes, and private code across multiple approaches.
Target learners who know the basics of Node and how it works, including modules and creating functions. Refresh your knowledge with tutorials or videos about Node before joining this course.
Explore chai, the popular assertion library for node.js unit testing, and learn why expect is preferred, how to use its three styles, and how plugins extend its capabilities.
Explore two ways to structure Node.js unit tests: a root test folder mirroring the app, or per-file tests named dot test.js, and run mocha with patterns via npm test.
Hi All,
These are the files used in the course, they have the project files AND test files, I suggest you delete / move test files somewhere for the moment while you go through the course and write your own tests along, only use them if you are stuck.
Learn how to test a node callback function with Mocha, validating the error-first pattern and a three result, using a delayed demo.js function and done signaling to pass two tests.
Test callbacks and promises in node.js unit tests by validating a promise-based add function, handling then, catch, and done, and illustrating proper error handling with mocha.
Rerun tests to confirm clarity, demonstrate returning a promise in tests, and verify the expectation that the result equals three during node.js unit testing.
Test promises with async await and chai as promised to verify asynchronous results end to end. Learn to use expect(...).to eventually equal, test rejections, and handle multiple conditions.
Explore sinon for test doubles, including spies and stubs, to isolate code in unit tests; install with npm install and save as a dev dependency.
Learn to use spies and test doubles in Node.js unit testing by spying on console.log and console.warn with sinon and sinon-chai, verifying calls, and restoring spies between tests.
Isolate code in unit tests by stubbing database calls so the function thinks it saved and prints the welcome message, while noting spies are less useful for unit testing.
Learn to test error handling by stubbing mongoose find by ID to yield an error, then assert an error exists, has the message, and that a result is not returned.
Test the users.update function by stubbing find by ID and saving updates, ensuring find is called with the correct ID, user.save is invoked, and errors are properly rejected.
Learn how to unit test users.resetPassword in Node.js by stubbing the mailer send password reset email function, validating the email input, and ensuring the reset email is sent.
Learn how to unit test utils.getHash by stubbing config secret and crypto, isolating the function, and chaining update and digest to produce a hash with md5.
Explore testing a Node.js order class by validating its constructor sets ref, user, items and computes subtotal, shipping, and total, with stubbed dates and method checks.
Learn how to measure code coverage with Istanbul / NYC in node.js unit tests. Install NYC, set up a coverage script, run tests, and examine per-file coverage, including uncovered lines.
Learn to run mocha tests from the root while excluding the node_modules folder to avoid recursive testing. Add the NYC folder to gitignore to keep your project clean.
Explore test driven development in Node.js unit testing, starting with a failing test, writing the minimum code to pass, and iterating with Mocha and Chai to ensure backwards compatibility.
Mocking Require Statements
This is a focused, in-depth course about unit testing, where we will look at lots of code, and learn how to test it. We will go through everything from the simplest JavaScript function to ES6 classes & private code testing. We will also learn how to test your code coverage, so you always know where you stand.
One of the goals of this course is to teach you how to think about unit testing, and not simply teach you the basic syntax or how to call an assertion.
We will go through most scenarios you will run into day to day, as well as address a number of issues and examples ignored by most unit testing courses available online.
You should have some node experience, you don't need to be an expert, but you should at least know the basics.
I am always looking to improve the course, please send me any feedback you may have directly!
Please go ahead and add me on LinkedIn, I love to connect with my students (I also love endorsements :D)