-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(expect, @jest/expect-utils): allow isA
utility to take a type argument
#13355
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"extends": "../../../tsconfig.json", | ||
"compilerOptions": { | ||
"composite": false, | ||
"noUnusedLocals": false, | ||
"noUnusedParameters": false, | ||
"skipLibCheck": true, | ||
|
||
"types": [] | ||
}, | ||
"include": ["./**/*"] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import {expectType} from 'tsd-lite'; | ||
import {isA} from '@jest/expect-utils'; | ||
|
||
// isA | ||
|
||
expectType<boolean>(isA('String', 'default')); | ||
expectType<boolean>(isA<number>('Number', 123)); | ||
|
||
const sample = {} as unknown; | ||
|
||
expectType<unknown>(sample); | ||
|
||
if (isA('String', sample)) { | ||
expectType<unknown>(sample); | ||
} | ||
|
||
if (isA<string>('String', sample)) { | ||
expectType<string>(sample); | ||
} | ||
|
||
if (isA<number>('Number', sample)) { | ||
expectType<number>(sample); | ||
} | ||
|
||
if (isA<Map<unknown, unknown>>('Map', sample)) { | ||
expectType<Map<unknown, unknown>>(sample); | ||
} | ||
|
||
if (isA<Set<unknown>>('Set', sample)) { | ||
expectType<Set<unknown>>(sample); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -185,7 +185,7 @@ class ArrayContaining extends AsymmetricMatcher<Array<unknown>> { | |
super(sample, inverse); | ||
} | ||
|
||
asymmetricMatch(other: Array<unknown>) { | ||
asymmetricMatch(other: unknown) { | ||
if (!Array.isArray(this.sample)) { | ||
throw new Error( | ||
`You must provide an array to ${this.toString()}, not '${typeof this | ||
|
@@ -257,8 +257,8 @@ class StringContaining extends AsymmetricMatcher<string> { | |
super(sample, inverse); | ||
} | ||
|
||
asymmetricMatch(other: string) { | ||
const result = isA('String', other) && other.includes(this.sample); | ||
asymmetricMatch(other: unknown) { | ||
const result = isA<string>('String', other) && other.includes(this.sample); | ||
Comment on lines
+260
to
+261
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is just an internal type, user facing external types do not change. The point is that internally At first it sounded strange, but reasons are very well explained in #7107 |
||
|
||
return this.inverse ? !result : result; | ||
} | ||
|
@@ -280,8 +280,8 @@ class StringMatching extends AsymmetricMatcher<RegExp> { | |
super(new RegExp(sample), inverse); | ||
} | ||
|
||
asymmetricMatch(other: string) { | ||
const result = isA('String', other) && this.sample.test(other); | ||
asymmetricMatch(other: unknown) { | ||
const result = isA<string>('String', other) && this.sample.test(other); | ||
|
||
return this.inverse ? !result : result; | ||
} | ||
|
@@ -297,6 +297,7 @@ class StringMatching extends AsymmetricMatcher<RegExp> { | |
|
||
class CloseTo extends AsymmetricMatcher<number> { | ||
private precision: number; | ||
|
||
constructor(sample: number, precision = 2, inverse = false) { | ||
if (!isA('Number', sample)) { | ||
throw new Error('Expected is not a Number'); | ||
|
@@ -311,8 +312,8 @@ class CloseTo extends AsymmetricMatcher<number> { | |
this.precision = precision; | ||
} | ||
|
||
asymmetricMatch(other: number) { | ||
if (!isA('Number', other)) { | ||
asymmetricMatch(other: unknown) { | ||
if (!isA<number>('Number', other)) { | ||
return false; | ||
} | ||
let result = false; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This and following changes comes from Prettier. CI does not catch them, because of
/* eslint-disable */
in this file.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah, then we should make sure to include this file in https://github.com/facebook/jest/blob/8759d63787b832af1fc9ac0ec2cc57b6f325ba9a/package.json#L103
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just check, I will send a separate PR removing that comment. Seems doable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we have purposefully not been running eslint on it to keep the diff from the jasmine equivalent down.
That might not be relevant 6-7 years later