Contributing to Jest-Extended

Hansal Bachkaniwala
Oct 4 · 3 min read

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

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:

  • toBeBetween
  • toBeAfterOrEqualTo
  • toBeBeforeOrEqualTo

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:

  1. Create a new subdirectory under the matchers directory.
  2. Add the following files to the subdirectory:
  • index.js - An export of the matcher in the format expected by Jest.
  • index.test.js - Test suite that uses the new matcher and makes sure it passes.
  • predicate.js - The function that tests the actual value meets the expected value/behavior.
  • predicate.test.js - Tests for the predicate. Both true/false cases must be covered.

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!

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

  1. Brushed up on my TypeScript programming skills.
  2. Learned how to write custom matchers for Jest.
  3. Learned to collaborate with other contributors.
  4. Learned how to write documentation for code.
  5. Learned how to write tests for code.
Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade