Skip to content

Commit 94935ea

Browse files
authored
Replace jest-get-type (#749)
1 parent 8db144b commit 94935ea

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+360
-206
lines changed

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,7 @@
6464
"typescript": "^5.0.0"
6565
},
6666
"dependencies": {
67-
"jest-diff": "^29.0.0",
68-
"jest-get-type": "^29.0.0"
67+
"jest-diff": "^29.0.0"
6968
},
7069
"engines": {
7170
"node": "^18.12.0 || ^20.9.0 || ^22.11.0 || >=23.0.0"

src/matchers/toBeAfter.ts

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,8 @@
1-
import { getType } from 'jest-get-type';
2-
31
export function toBeAfter(actual: unknown, after: Date) {
42
// @ts-expect-error OK to have implicit any for this
53
const { printReceived, matcherHint } = this.utils;
64

7-
if (getType(actual) !== 'date') {
8-
throw new Error(
9-
matcherHint('.toBeAfter', 'received', '') +
10-
'\n\n' +
11-
'Expected value to be of type Date but received:\n' +
12-
` ${printReceived(actual)}`,
13-
);
14-
}
15-
16-
// @ts-expect-error getType provides the type check
17-
const pass = actual > after;
5+
const pass = actual instanceof Date && actual > after;
186

197
return {
208
pass,

src/matchers/toBeAfterOrEqualTo.ts

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,8 @@
1-
import { getType } from 'jest-get-type';
2-
31
export function toBeAfterOrEqualTo(actual: unknown, expected: Date) {
42
// @ts-expect-error OK to have implicit any for this
53
const { printReceived, matcherHint } = this.utils;
64

7-
if (getType(actual) !== 'date') {
8-
throw new Error(
9-
matcherHint('.toBeAfterOrEqualTo', 'received', '') +
10-
'\n\n' +
11-
'Expected value to be of type Date but received:\n' +
12-
` ${printReceived(actual)}`,
13-
);
14-
}
15-
16-
// @ts-expect-error getType provides the type check
17-
const pass = actual >= expected;
5+
const pass = actual instanceof Date && actual >= expected;
186

197
return {
208
pass,

src/matchers/toBeBefore.ts

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,8 @@
1-
import { getType } from 'jest-get-type';
2-
31
export function toBeBefore(actual: unknown, expected: Date) {
42
// @ts-expect-error OK to have implicit any for this
53
const { matcherHint, printReceived } = this.utils;
64

7-
if (getType(actual) !== 'date') {
8-
throw new Error(
9-
matcherHint('.toBeBefore', 'received', '') +
10-
'\n\n' +
11-
'Expected value to be of type Date but received:\n' +
12-
` ${printReceived(actual)}`,
13-
);
14-
}
15-
16-
// @ts-expect-error getType provides the type check
17-
const pass = actual < expected;
5+
const pass = actual instanceof Date && actual < expected;
186

197
return {
208
pass,

src/matchers/toBeBeforeOrEqualTo.ts

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,8 @@
1-
import { getType } from 'jest-get-type';
2-
31
export function toBeBeforeOrEqualTo(actual: unknown, expected: Date) {
42
// @ts-expect-error OK to have implicit any for this
53
const { matcherHint, printReceived } = this.utils;
64

7-
if (getType(actual) !== 'date') {
8-
throw new Error(
9-
matcherHint('.toBeBeforeOrEqualTo', 'received', '') +
10-
'\n\n' +
11-
'Expected value to be of type Date but received:\n' +
12-
` ${printReceived(actual)}`,
13-
);
14-
}
15-
16-
// @ts-expect-error getType provides the type check
17-
const pass = actual <= expected;
5+
const pass = actual instanceof Date && actual <= expected;
186

197
return {
208
pass,

src/matchers/toBeBetween.ts

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,8 @@
1-
import { getType } from 'jest-get-type';
2-
31
export function toBeBetween(actual: unknown, startDate: Date, endDate: Date) {
42
// @ts-expect-error OK to have implicit any for this
53
const { matcherHint, printReceived } = this.utils;
64

7-
if (getType(actual) !== 'date') {
8-
throw new Error(
9-
matcherHint('.toBeBetween', 'received', '') +
10-
'\n\n' +
11-
'Expected value to be of type Date but received:\n' +
12-
` ${printReceived(actual)}`,
13-
);
14-
}
15-
16-
// @ts-expect-error getType provides the type check
17-
const pass = actual >= startDate && actual <= endDate;
5+
const pass = actual instanceof Date && actual >= startDate && actual <= endDate;
186

197
return {
208
pass,

src/matchers/toBeDate.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
import { getType } from 'jest-get-type';
2-
31
export function toBeDate(actual: unknown) {
42
// @ts-expect-error OK to have implicit any for this
53
const { matcherHint, printReceived } = this.utils;
64

7-
// @ts-expect-error getType provides the type check
8-
const pass = getType(actual) === 'date' && !isNaN(actual);
5+
const pass = actual instanceof Date && !isNaN(actual.getTime());
96

107
return {
118
pass,

src/matchers/toBeDateString.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
import { getType } from 'jest-get-type';
2-
31
export function toBeDateString(actual: unknown) {
42
// @ts-expect-error OK to have implicit any for this
53
const { matcherHint, printReceived } = this.utils;
64

7-
// @ts-expect-error getType provides the type check
8-
const pass = getType(actual) === 'string' && !isNaN(Date.parse(actual));
5+
const pass = typeof actual === 'string' && !isNaN(Date.parse(actual));
96

107
return {
118
pass,

src/matchers/toBeEmptyObject.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
import { getType } from 'jest-get-type';
2-
31
export function toBeEmptyObject(actual: unknown) {
42
// @ts-expect-error OK to have implicit any for this
53
const { printReceived, matcherHint } = this.utils;
64

7-
// @ts-expect-error getType provides the type check
8-
const pass = getType(actual) === 'object' && Object.keys(actual).length === 0;
5+
const pass =
6+
typeof actual === 'object' && actual !== null && !Array.isArray(actual) && Object.keys(actual).length === 0;
97

108
return {
119
pass,

src/matchers/toBeHexadecimal.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
import { getType } from 'jest-get-type';
2-
31
export function toBeHexadecimal(actual: unknown) {
42
// @ts-expect-error OK to have implicit any for this
53
const { printReceived, matcherHint } = this.utils;
64

7-
// @ts-expect-error getType provides the type check
8-
const pass = (getType(actual) === 'string' && longRegex.test(actual)) || shortRegex.test(actual);
5+
const pass =
6+
(typeof actual === 'string' && longRegex.test(actual)) || (typeof actual === 'string' && shortRegex.test(actual));
97

108
return {
119
pass,

0 commit comments

Comments
 (0)