Skip to content

Latest commit

 

History

History
70 lines (43 loc) · 3.49 KB

5.5.4-code-coverage.md

File metadata and controls

70 lines (43 loc) · 3.49 KB
docs/5-software-development-practices/5.5.4-code-coverage.md
category estReadingMinutes exercises
Software Quality
20
name description estMinutes technologies
Achieve 100% code coverage
Install node dependencies for code coverage (jest) and get 100% code coverage for a given module and tests.
60
Node.js
Jest

Code Coverage:

Code coverage is the percentage of code which is covered by automated tests. Code coverage measurement simply determines which statements in a body of code have been executed through a test run, and which statements have not. There are many ways to check code coverage and they all vary depending on the language you are using. For the Go language coverage testing is built in so you can use the go test -cover command. In many other languages you will have to use a third party tool like Coveralls, Codecov, or Istanbul.

Exercise 1:

You will be using Jest to test the code coverage in the provided code. You can find the code in the bootcamp repository inside the examples folder.

The following code is a series of functions written in Javascript (simple.js):

sampleCode

The following code contains the unit tests that will be testing the Javascript functions (simple.test.js):

testCode

Please perform the following:

  • Install Node

      brew install node
    
  • Install Jest to your node project npm install jest

After installing those dependencies take a moment to examine your package.json file. Note that jest is currently in the section called "dependencies". This may not be ideal for a production environment, because if you set up a docker file or some other container to work with this file you may end up downloading a bunch of dependencies you don't necessarily need.

Try redoing the previous steps, but install the files as a developer dependency. Once you have done that take a look at your package.json file.

?>Note: When installing Jest with the npm command, use the -D (or --save-dev) tag to install as a developer dependency.

The provided Javascript package.json file will have everything you need in order to pass the Jest tests; however, you will need to add to the file in order to get Jest to check your coverage.

package

Inside your package.json file under the “scripts” section, add a section called “coverage” and use the following command

jest --coverage

Make sure that your “test” section is running the following command

jest

Run your code with the following command:

npm run coverage

Exercise 2:

Now that you have Jest running and you can check your code coverage. Add another unit test to the simple.test.js file so that you achieve 100% coverage.

Deliverables

  • How does code coverage help developers?
  • What are some scenarios that make it harder to achieve 100% coverage?
  • Do you think it's required to have 100% coverage for it to be effective?