Skip to main content

Date

.toBeDate()

Use .toBeDate when checking if a value is a Date.

test('passes when value is a date', () => {
  expect(new Date()).toBeDate();
  expect('01/01/2018').not.toBeDate();
  expect(new Date('01/01/2018')).toBeDate();
  expect(undefined).not.toBeDate();
});

Open browser consoleTests

.toBeValidDate()

Use .toBeValidDate when checking if a given Date object is valid.

test('passes when Date is valid', () => {
  expect(new Date()).toBeValidDate();
  expect('01/01/2018').not.toBeValidDate();
  expect(new Date('01/01/2018')).toBeValidDate();
  expect(new Date('01/90/2018')).not.toBeValidDate();
  expect(undefined).not.toBeValidDate();
});

Open browser consoleTests

.toBeAfter(date)

Use .toBeAfter when checking if a date occurs after date.

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

Open browser consoleTests

.toBeBefore(date)

Use .toBeBefore when checking if a date occurs before date.

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

Open browser consoleTests

.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'));
  expect(new Date('01/01/2019')).toBeAfterOrEqualTo(new Date('01/01/2019'));
  expect('01/01/2018').not.toBeAfterOrEqualTo(new Date('01/01/2019'));
});

Open browser consoleTests

.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'));
  expect(new Date('01/01/2018')).toBeBeforeOrEqualTo(new Date('01/01/2018'));
  expect('01/01/2019').not.toBeBeforeOrEqualTo(new Date('01/01/2018'));
});

Open browser consoleTests

.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'));
 expect(new Date('05/01/2019')).toBeBetween(new Date('05/01/2019'), new Date('10/01/2019'));
 expect(new Date('01/01/2019')).not.toBeBetween(new Date('05/01/2019'), new Date('10/01/2019'));
});

Open browser consoleTests