Example project to allow me to show how best to unit test a JavaScript Express application.
Clone this repository and install the dependencies as follows:
git clone git@github.com:MarcL/js-unit-testing-framework.git
npm install
The code sets up a basic Express server with a few routes and some tests which cover testing the server setup. Run the test suite using npm:
npm test
Take a look in the test
directory to see all of the test code. There are lots of examples of different types of tests and how to create them.
Some examples of how to test asynchronous functions and promises, including some tips and tricks and gotchas.
- Timeout because
done
callback isn't called when function succeeds - Passing test but slow because timer isn't stubbed
- Passing test and fast because timer is stubbed
- Timeout because
done
callback isn't called when function throws an error - Passing test to call
done
callback when function throws an error
- Test passes incorrectly because Promise isn't returned
- Passing test because promise is returned
- Passing test because
done
callback is called after resolution - Passing test because
done
callback is called after resolution usingchai-as-promised
syntax
- Test passes incorrectly because Promise isn't returned
- Failing test because rejected Promise error isn't caught
- Passing test because Promise is returned and rejection is caught
- Passing test because Promise rejection is caught and
done
callback is called - Passing test because Promise rejection is caught and
done
callback is called usingchai-as-promised
syntax
- Passing test but is slow due to Promise function in chain taking a long time
- Passing test and much faster as longer function is now stubbed to execute immediately
See LICENSE