- For fun
- Tutorial link: Learn Test Driven Development (in JavaScript) (on github)
- 3-step process
- write a failing test
understand the requirements/story well enough to write a est for what you expect
- make the test pass
write only the code you need to make the (failing)test pass
while ensuring your existing/previous test all still pass - refactor the code you wrote
tidy up the code you wrote to make it simpler for yourself and colleagues to understand
- write a failing test
- We almost do not need to install anything.
- For a live server, you can install and use it by:
- don't need live-reload:
python -m http.server 9000 --bind 0.0.0.0
- do need it:
npm install live-server --save-dev && ./node_modules/.bin/live-server --port=8000
- don't need live-reload:
- For the code coverage part, a short HOWTO on
blanket
lib- see Getting Started (blanket)
- add attr to the script you wanna run coverage on:
<script src="sourceScript.js" data-cover></script>
- For the Node.js (server-side) tests
- install libraries:
npm install qunitjs qunit-tap istanbul --save-dev
qunitjs
: core libraryqunit-tap
: for command-line outputistanbul
: code coverage (server-side) (better than blanket lib)
- actually running them
- testing only:
npm test
- test coverage included
./node_modules/.bin/istanbul cover TEST_FILE.js
ORnpx istanbul cover test_feauture.js
- after running the cmd you could see the detailed coverage report at
./coverage/../index.html
- testing only:
- install libraries:
- For the JSDoc part
- install library:
npm install jsdoc --save-dev
- run it:
./node_modules/.bin/jsdoc YOUR_MAIN_SCRIPT.js
ORnpx jsdoc YOUR_MAIN_SCRIPT.js
- check the result:
open ./out/global.html#getChange
- install library:
- Caveats when you use
blanket
test coverage library- It cannot identify the bad code if it was written in one line (e.g.
if (..) { bad stuff }
)
- It cannot identify the bad code if it was written in one line (e.g.
- The TDD partly mostly happen in the two
.js
files (and theindex.html
). - Add code coverage badge at Codecov.io, here's something you need to know
- Available on Github, BitBucket, Gitlab
- It supports public repo by default, requires additional perms for private repo