Skip to content
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

Migrate expect to typescript #7919

Merged
merged 48 commits into from Feb 20, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
3e04c16
Rename src files
natealcedo Feb 17, 2019
43320f3
Migrate utils.ts
natealcedo Feb 17, 2019
bf35c81
Add tsconfig
natealcedo Feb 17, 2019
110729b
Add jest-types reference
natealcedo Feb 17, 2019
6ce71f6
Implement types.ts for expect
natealcedo Feb 17, 2019
fc978a6
Add a types key in package.json
natealcedo Feb 17, 2019
221074c
Migrate toThrowMatchers
natealcedo Feb 17, 2019
1cd1006
Migrate spyMatchers
natealcedo Feb 17, 2019
9e0d8e6
Migrate matchers
natealcedo Feb 17, 2019
ab7d1e1
Migrate jasmineUtils
natealcedo Feb 17, 2019
73042ba
migrate fakechalk
natealcedo Feb 17, 2019
30efca3
Migrate extractExpectedAssertionsErrors
natealcedo Feb 17, 2019
032c6ee
Migrate asymmetricMatchers
natealcedo Feb 17, 2019
f4f5536
Migrate jestMatchersObject
natealcedo Feb 17, 2019
bf8198c
Upate Expectation object definition
natealcedo Feb 17, 2019
a62f3a4
Migrate expect/index.ts
natealcedo Feb 17, 2019
428620d
Add a diff file since it wont fit in the PR description
natealcedo Feb 17, 2019
1d9fe02
Run eslint
natealcedo Feb 17, 2019
feb8e86
Declare jest types as a depenency
natealcedo Feb 17, 2019
b221710
import type localy
natealcedo Feb 17, 2019
4258222
Sort dependencies in package.json
natealcedo Feb 18, 2019
2801634
Change returnInput argument type
natealcedo Feb 18, 2019
88cb209
Type syncResult and asyncResult
natealcedo Feb 18, 2019
8ab2dae
Remove MatcherHintOptions in types.ts
natealcedo Feb 18, 2019
59ab17d
Type this in matchers to use MatcherState and options to use MatcherH…
natealcedo Feb 18, 2019
a3e1d44
Remove flow declaration
natealcedo Feb 18, 2019
a48b31f
Better type asmmetricMathchers
natealcedo Feb 18, 2019
27b2049
Export Tester type
natealcedo Feb 18, 2019
282b15e
Improve types for jasmineUtils
natealcedo Feb 18, 2019
2d41e89
Improve types for jestMatchersObject
natealcedo Feb 18, 2019
dddf3d2
Improve typings for matchers
natealcedo Feb 18, 2019
d99c26c
Improve spyMatchers typings
natealcedo Feb 18, 2019
7f5575d
Improve types for toThrowMatchers
natealcedo Feb 18, 2019
a8ff374
Improve types for utils.ts
natealcedo Feb 18, 2019
7f2aef4
Update diff file
natealcedo Feb 18, 2019
602e34c
Add a typeguard for jasmineUtils
natealcedo Feb 19, 2019
bf61f89
Update createToThrowErrorMatchingSnapshotMatcher typing
natealcedo Feb 19, 2019
4ede592
Type getMessage and isPromise
natealcedo Feb 19, 2019
72feb3e
Type case matcher call
natealcedo Feb 19, 2019
bf24385
tweak
SimenB Feb 19, 2019
db1389d
remove diff file
SimenB Feb 19, 2019
90dffa8
tweak promise type guard
SimenB Feb 19, 2019
cc42c7c
revert ignorefile change
SimenB Feb 19, 2019
8fef3e4
prettier on jasmine utils
SimenB Feb 19, 2019
fbb94e3
fix copyright header
SimenB Feb 20, 2019
a432bb5
Merge branch 'master' into migrate/expect
SimenB Feb 20, 2019
b51b16a
update changelog
SimenB Feb 20, 2019
e14c798
small type adjustments; use jestMatcherUtils for MatcherState
thymikee Feb 20, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/expect/src/index.ts
Expand Up @@ -405,6 +405,6 @@ expect.assertions = assertions;
expect.hasAssertions = hasAssertions;
expect.getState = getState;
expect.setState = setState;
(expect as any).extractExpectedAssertionsErrors = extractExpectedAssertionsErrors;
expect.extractExpectedAssertionsErrors = extractExpectedAssertionsErrors;

export = expect as Expect;
10 changes: 5 additions & 5 deletions packages/expect/src/jestMatchersObject.ts
Expand Up @@ -53,16 +53,16 @@ export const setMatchers = (
if (!isInternal) {
// expect is defined

class CustomMatcher extends AsymmetricMatcher<Array<unknown>> {
constructor(inverse: boolean = false, ...sample: Array<unknown>) {
class CustomMatcher extends AsymmetricMatcher<[unknown, unknown]> {
constructor(inverse: boolean = false, ...sample: [unknown, unknown]) {
super(sample);
this.inverse = inverse;
}

asymmetricMatch(other: unknown) {
const {pass} = matcher(
other,
...(this.sample as [unknown, unknown]),
...this.sample,
) as SyncExpectationResult;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just copying over from the original code. This typing seems more accurate than my previous change. comments are appreciated


return this.inverse ? !pass : pass;
Expand All @@ -81,12 +81,12 @@ export const setMatchers = (
}
}

expect[key] = (...sample: Array<unknown>) =>
expect[key] = (...sample: [unknown, unknown]) =>
new CustomMatcher(false, ...sample);
if (!expect.not) {
expect.not = {};
}
expect.not[key] = (...sample: Array<unknown>) =>
expect.not[key] = (...sample: [unknown, unknown]) =>
new CustomMatcher(true, ...sample);
}
});
Expand Down
12 changes: 4 additions & 8 deletions packages/expect/src/matchers.ts
Expand Up @@ -566,7 +566,7 @@ const matchers: MatchersObject = {
toHaveProperty(
this: MatcherState,
object: object,
keyPath: string | Array<any>,
keyPath: string | Array<string>,
value?: unknown,
) {
const valuePassed = arguments.length === 3;
Expand Down Expand Up @@ -655,11 +655,7 @@ const matchers: MatchersObject = {
return {message, pass};
},

toMatch(
this: MatcherState,
received: string,
expected: string | RegExp | any,
) {
toMatch(this: MatcherState, received: string, expected: string | RegExp) {
if (typeof received !== 'string') {
throw new Error(
matcherErrorMessage(
Expand All @@ -673,8 +669,8 @@ const matchers: MatchersObject = {
}

if (
!(expected && typeof expected.test === 'function') &&
!(typeof expected === 'string')
!(typeof expected === 'string') &&
!(expected && typeof expected.test === 'function')
) {
throw new Error(
matcherErrorMessage(
Expand Down
7 changes: 2 additions & 5 deletions packages/expect/src/types.ts
Expand Up @@ -6,6 +6,7 @@
*
*/
import {Config} from '@jest/types';
import * as jestMatcherUtils from 'jest-matcher-utils';

export type SyncExpectationResult = {
pass: boolean;
Expand Down Expand Up @@ -45,11 +46,7 @@ export type MatcherState = {
promise: string;
suppressedErrors: Array<Error>;
testPath?: Config.Path;
// This is output from `jest-matcher-utils` plus iterableEquality, subsetEquality
// Type it correctly when moving it to `expect`
utils: {
printExpected: (value: unknown) => string;
printReceived: (value: unknown) => string;
utils: typeof jestMatcherUtils & {
iterableEquality: Tester;
subsetEquality: Tester;
};
Expand Down
1 change: 1 addition & 0 deletions packages/jest-snapshot/src/index.ts
Expand Up @@ -20,6 +20,7 @@ import SnapshotState from './State';
import {addSerializer, getSerializers} from './plugins';
import * as utils from './utils';

// TODO: use MatcherState directly from `expect` once whole project is migrated
type Context = Matchers.MatcherState & {
snapshotState: SnapshotState;
};
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-types/src/Matchers.ts
Expand Up @@ -5,7 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

// TODO: Move this to `expect` when it's migrated
// TODO: Remove this when whole project is migrated
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cannot do it currently, because of export = stuff, and importing from expect/src/types seemed fishy (and caused TS errors, oh well who's got time for that XD)


import {Path} from './Config';

Expand Down