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(); });
Tests
.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(); });
Tests
.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')); });
Tests
.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')); });
Tests
.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')); });
Tests
.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')); });
Tests
.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')); });
Tests