Contributing to Jest-Extended

For my first contribution for Hacktoberfest, I chose an issue from the Jest-Extended project. It is a project that provides custom matchers (also called assertions) that can be used in the Jest testing framework. This blog post will briefly discuss how I tackled this issue and what I learned in the process.

The Issue Explained

Image for post
Image for post
Feature Request Issue

This issue is a feature request that stated the need for new date matchers that can be used in Jest. A matcher is a method/function which compares the expected result with the actual result and returns a boolean (true/false) value. These matchers form the basis of testing frameworks like Jest. The Jest project provides many matchers but users often require new ones to meet project-specific requirements. I chose to contribute three new date matchers requested in this issue:

The Process

To get started, I first read the contribution guidelines and set up my development environment. Then I read a few code examples from existing matchers and listed the steps I would need to create and test a new matcher. They were as follows:

3. Update the documentation.

4. Add type definition to types/index.d.ts file.

After this, I coded the files listed above (using the TypeScript language) and tested them. When all tests passed successfully, the code was committed (commits were squashed), pushed and a pull request was created. The Codecov bot ran the test suites which passed and code coverage was 100%. Hooray!

Image for post
Image for post
The Pull Request

Here are some examples of how my matchers can be used in Jest:

toBeAfterOrEqualTo(date)

Use .toBeAfterOrEqualTo when checking if a date equals to or occurs after date.

test('passes when input is equal to or after date', () => { expect(new Date('01/01/2019')).toBeAfterOrEqualTo(new Date('01/01/2018')); 
});

.toBeBeforeOrEqualTo(date)

Use .toBeBeforeOrEqualTo when checking if a date equals to or occurs before date.

test('passes when input is equal to or before date', () => { expect(new Date('01/01/2018')).toBeBeforeOrEqualTo(new Date('01/01/2019'));
});

.toBeBetween(startDate, endDate)

Use .toBeBetween when checking if a date equals or occurs after startDate and equals or occurs before endDate.

test('passes when input is in given date range', () => { 
expect(new Date('05/01/2019')).toBeBetween(new Date('01/01/2019'), new Date('10/01/2019');
});

Learning Outcomes

Written by

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store