Skip to content

Repository that contains code in Node.js from the book The Art of Unit Testing, Second Edition by Roy Osherove

License

Notifications You must be signed in to change notification settings

devcorpio/the-art-of-unit-testing

Repository files navigation

The Art of Unit Testing, Second Edition

The Art of Unit Testing, Second Edition

The Art of Unit Testing is a book written by Roy Osherove

The book code examples are written in C# and the tools that appears in it are from the .NET community.

Goal of this repository

Node.js

I would like that people that works using Node.js could enjoy of the knowledge that this book offers to its readers. Therefore I going to use Node.js to write the code examples and I going to use tools typically related with it. 😍

The style of the code and the chosen tools are 100% my decision. (The good and the bad parts! 😚)

How study the repository

  1. For every chapter of the book has I created a directory where appears the final version of the code in the mentioned chapter.

  2. Every commit has a reference to the chapter related. Any change I needed to do has a commit, with the objective of follow the flow of the book.

Note: If you want to open the links in another tab, just do a CTRL+click on the link.

Chapters

  1. The basics of unit testing

    git commits done during the chapter :shipit:

    - Initial commit
    - In order to commit formatted code I installed prettier, pretty-quick and husky
    - update README
    - preparing the simpleParser example, creating InvalidOperation custom error
    - creating simpleParser example
    - creating a test manually to do basic tests to simpleParser, I'm not using unit testing frameworks, yet

  2. A first unit test

    git commits done during the chapter :shipit:

    - starting LogAn, the project that we are going to use in the next chapters
    - install jest
    - renaming homemade test of the chapter 01 to avoid conflicts with jest
    - creating logAnalyzer and its test, that show us that the SUT have a bug
    - fix isValidLogFileName in order to fix the test
    - adding two more tests, one of them intetionally fails
    - fix in isValidLogFilename to fix the test
    - refactoring the code of thest using the parameterized tests technique
    - adding setup to the test
    - returns error if the filename is empty
    - add test that assert that the error is thrown
    - adding state logAnalyzer
    - first state-based test for logAnalyzer
    - add a inmemory calculator to continuing trying state-based testing
    - add a memory calculator in order to test it with state-based testing
    - update readme
    - remove .vscode from repository

  3. Using stubs to break dependencies

    git commits done during the chapter :shipit:

    - copy code from chapter 02 to the chapter 03 folder in order to continue with the book
    - install @types/node, with that, I can work with types and node.js modules without vscode warnings
    - check the validity of the file extension in a disk file, that creates an external dependency, unit tests are broken
    - fixing logAnalyzer tests, right now they are integration tests and not unit tests, meh
    - extracting a factory function that touches the filesystem and calling it
    - creating a fake extension manager, the name of a fake is because we can use it as a stub or a mock, depending of the test.
    - create a new fake that is ready to be configurable to use in test
    - fixing alwaysValidFakeExtensionManager, I didn't create the function return
    - I created a seam in logAnalyzer, that seam enable the possibility of inject the dependency while are calling the function
    - using the fakeManagerExtension to fix the failed test from the previous commit
    - creating a extension manager factory that allows to set the extension manager to return before execute it, the default manager it returns is fileExtensionManager
    - using extensionManagerFactory I created an integration test, because the test is making use of a external dependency, the filesystem
    - using extensionManagerFactory I created an unit test, because the test is making use of a fake extension manager, therefore I'm not using the filesystem that is an external dependency
    - I have changed the isValid method from the fakes to return a promise instead of a boolean
    - In order to use the technique Extract and Override I needed to create a new file using logAnalyzer as a class and create a virtual method
    - testing the the new class using the technique Extract and Override

  4. Interaction testing using mock objects

    git commits done during the chapter :shipit:

    - copy code from chapter 03 to the chapter 04 folder in order to continue with the book
    - organizing a bit more the files to improve the 'first glance' effect of LogAn folder
    - create a new object that fake a call to a web service
    - create a basic webservice in order to create an example that I want to create from the book
    - adding a parameter that allows to pass an object webService to logAnalyzer
    - creating the real webService connector
    - fixing fakeWebService, create getter and return both object with the two functions
    - create a unit test that use fakeWebService as a mock
    - adding another fake named emailService, and create a new test where the webService is used as stub and the emailService as a mock

  5. Isolation (mocking) frameworks

    git commits done during the chapter :shipit:

    - copy logAnalyzer.js, errors and fakes from the folder of chapter 04 in order to continue with the book
    - creating a test that use a fake handwritten
    - instead of use a handwriting fake I create it using jest mocking module!
    - check that the logError method is called with the expected error message using jest mocking system functions

  6. Digging deeper into isolation frameworks

    git commits done during the chapter :shipit:

    No commits here, this chapter go deep in the explanation about isolation frameworks, interesting concepts! 😁

  7. test hierarchies and organization

    git commits done during the chapter :shipit:

    - initial code to create a test api for the application, the name of the first strategy is abstract test infrastructure class pattern
    - refactored solution based in abstract test infrastructure class pattern

    • Also Wonderful explanations about concepts as:

      1. Focus in the importance of separation of unit tests and integration tests.
      2. Separate them by speed: slows, fasts, etc
      3. Create nightly builds, CI builds, etc
      4. More and more 😁 if you want to know all, buy the book

  8. The pillars of good unit tests

    git commits done during the chapter :shipit:

    - No commits here, in this chapter Roy speaks deeply about:

     1. Writing trustworthy tests
     2. Writing maintainable tests
     3. Writing readable tests
     4. Exploring naming conventions for unit tests.
    

    Thanks to the content of this chapter I have said to myself many times "aha", this chapter by itself triggers a message,and the message is: "buy the book!" 😁 I enjoyed a lot this chapter.

  9. Integration unit testing into the organization

    git commits done during the chapter :shipit:

    - No commits here, wonderful chapter!, this book is a drug for me(a good drug!) πŸ˜„

  10. Working with legacy code

    git commits done during the chapter :shipit:

    - No commits here, several amazing concepts! πŸ˜„ πŸ˜„

  11. Design and testability

    git commits done during the chapter :shipit:

    - No commits here. I finished the read and study of the book, to read this book should be a must in my opinion

😁 😁 😁

How to use

Setup

Install all the dependencies:

npm install

If you want to execute the all tests I created:

npm run test

Final thoughts

I hope you enjoy the repository as much I while I was writing it πŸ˜ƒ

I strongly encourage that you should buy the book, it is a masterpiece.

About

Repository that contains code in Node.js from the book The Art of Unit Testing, Second Edition by Roy Osherove

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published