Improving test coverage for Telescope

Continuing from my last blog, I decide I wanted to try my hand in creating some tests in Jest. Currently there are two main areas of testing, improving coverage and creating regression testing. I wanted to try my hand in coverage. Luckily, Jest provides information about coverage:


Coverage of Telescope

Looking at the main issue that first brought up test coverage, I decide to work on one of the files to help improve test coverage:

--------------------------|----------|----------|----------|----------|-------------------|
File                      |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
--------------------------|----------|----------|----------|----------|-------------------|
All files                 |    74.39 |    62.24 |    67.44 |    73.86 |                   |
 src                      |    68.02 |     61.9 |    62.16 |    67.07 |                   |
  config.js               |      100 |      100 |      100 |      100 |                   |
  count-blog-domain.js    |    95.83 |      100 |      100 |    95.65 |                62 |
  email-sender.js         |    14.29 |        0 |        0 |    14.29 |... 42,50,51,52,54 |
  feed-parser.js          |      100 |      100 |      100 |      100 |                   |
  feed-queue.js           |      100 |      100 |      100 |      100 |                   |
  github-url.js           |    96.55 |    85.71 |      100 |    96.15 |                26 |
  inactive-blog-filter.js |    13.04 |        0 |       10 |    13.04 |... 30,131,132,135 |
  sentiment-analysis.js   |      100 |      100 |      100 |      100 |                   |
  spam-checker.js         |      100 |      100 |      100 |      100 |                   |
  storage.js              |    88.89 |      100 |    66.67 |    88.89 |                23 |
  summarize.js            |      100 |        0 |      100 |      100 |                 3 |
  text-parser.js          |      100 |      100 |      100 |      100 |                   |
  utils.js                |      100 |      100 |      100 |      100 |                   |
  word-counter.js         |      100 |      100 |      100 |      100 |                   |
 src/analysis             |    85.37 |    73.91 |      100 |    85.37 |                   |
  basic_analysis.js       |    85.37 |    73.91 |      100 |    85.37 | 20,22,34,35,40,82 |
 src/backend/web          |      100 |      100 |      100 |      100 |                   |
  app.js                  |      100 |      100 |      100 |      100 |                   |
 src/backend/web/routes   |      100 |      100 |      100 |      100 |                   |
  admin.js                |      100 |      100 |      100 |      100 |                   |
  index.js                |      100 |      100 |      100 |      100 |                   |
 src/lib                  |      100 |       50 |      100 |      100 |                   |
  redis.js                |      100 |       50 |      100 |      100 |                11 |
 utils                    |       75 |       40 |      100 |       75 |                   |
  logger.js               |       75 |       40 |      100 |       75 |             13,22 |
--------------------------|----------|----------|----------|----------|-------------------|

Test Suites: 1 skipped, 14 passed, 14 of 15 total
Tests:       4 skipped, 53 passed, 57 total
Snapshots:   0 total
Time:        6.44s, estimated 7s

The weakest test coverage file is inactive-blog-filter.js so I decide to work on that.


I first searched to make sure if this issue wasn’t already filed on this file already. To my luck this issue already was. The issue was already assigned to Silvyre, but the issue hasn’t been active for 10 days. I decide to ask Silvyre if I can try my hand on working on one of the functions for inactive-blog-filter.js which he gave me the OK. The function I will be testing is called dateDiff.

Creating test and testing

The dateDiff function returns back in milliseconds the difference between the current date with the given date:

/**
 * Simple function that takes the difference between current date and post date
 * If difference is greater than threshold, the blog is considered inactive
 * @param {Date} postDate - javascript date object of the post being checked
 * @returns {number} - date difference in milliseconds
 */
function dateDiff(postDate) {
  const currentDate = new Date();
  return currentDate - postDate;
}

Looking at the coverage files produce by Jest we can see the coverage of the dateDiff function:

The coverage of the inactive-blog-filter.js :


Creating the test for the dateDiff functions, I decide to test 3 possible inputs. If postDate is before currentDate, the same, or after. Creating the test wasn’t difficult following the conventions of other test:

describe('Testing dateDiff function', () => {
  it('dateDiff should return a positive value if postDate(parameter) is before currentDate', () => {
    expect(inactiveFilter.dateDiff(Date.now() - 1)).toBeGreaterThan(0);
  });

  it('dateDiff should return a negative value if postDate(parameter) is after currentDate', () => {
    expect(inactiveFilter.dateDiff(Date.now() + 1)).toBeLessThan(0);
  });

  it('dateDiff should return a zero value if postDate(parameter) is the same as currentDate', () => {
    expect(inactiveFilter.dateDiff(Date.now())).toEqual(0);
  });
});

In the beginning I was getting errors from Jest saying the function dateDiff could not be found. It took a little bit but I realize the reason was because dateDiff was not exported from the inactive-blog-filter.js, so I added this line to fix that:

exports.dateDiff = dateDiff;

I ran npm test and everything seem to work fine. Checking the coverage reports, inactive-blog-filter.js improve around 10% which was great:


Coverage:

Creating pull request

I am creating this pull request after learning about how to review pull request in class. I tried to create a pull request that would be easy to review. Linking to the issue, and providing pictures of what I have done. I did not explain the code, since I felt my code was pretty self explanatory even without prior knowledge you should be able to tell what is being tested in the dateDiff function.


Thanks to jerryshueh and Silvyre for reviewing my pull request , I was able to get it merged to master very quickly


This is a very small step into improving the test coverage of telescope but progress nonetheless. This is smaller pull request then I thought I would do but that’s because my external pull request took longer then I thought. Hopefully if I am not busy with other courses I can create more test for telescope.

References:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s