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
Feb 17, 2019
43320f3
Migrate utils.ts
Feb 17, 2019
bf35c81
Add tsconfig
Feb 17, 2019
110729b
Add jest-types reference
Feb 17, 2019
6ce71f6
Implement types.ts for expect
Feb 17, 2019
fc978a6
Add a types key in package.json
Feb 17, 2019
221074c
Migrate toThrowMatchers
Feb 17, 2019
1cd1006
Migrate spyMatchers
Feb 17, 2019
9e0d8e6
Migrate matchers
Feb 17, 2019
ab7d1e1
Migrate jasmineUtils
Feb 17, 2019
73042ba
migrate fakechalk
Feb 17, 2019
30efca3
Migrate extractExpectedAssertionsErrors
Feb 17, 2019
032c6ee
Migrate asymmetricMatchers
Feb 17, 2019
f4f5536
Migrate jestMatchersObject
Feb 17, 2019
bf8198c
Upate Expectation object definition
Feb 17, 2019
a62f3a4
Migrate expect/index.ts
Feb 17, 2019
428620d
Add a diff file since it wont fit in the PR description
Feb 17, 2019
1d9fe02
Run eslint
Feb 17, 2019
feb8e86
Declare jest types as a depenency
Feb 17, 2019
b221710
import type localy
Feb 17, 2019
4258222
Sort dependencies in package.json
Feb 18, 2019
2801634
Change returnInput argument type
Feb 18, 2019
88cb209
Type syncResult and asyncResult
Feb 18, 2019
8ab2dae
Remove MatcherHintOptions in types.ts
Feb 18, 2019
59ab17d
Type this in matchers to use MatcherState and options to use MatcherH…
Feb 18, 2019
a3e1d44
Remove flow declaration
Feb 18, 2019
a48b31f
Better type asmmetricMathchers
Feb 18, 2019
27b2049
Export Tester type
Feb 18, 2019
282b15e
Improve types for jasmineUtils
Feb 18, 2019
2d41e89
Improve types for jestMatchersObject
Feb 18, 2019
dddf3d2
Improve typings for matchers
Feb 18, 2019
d99c26c
Improve spyMatchers typings
Feb 18, 2019
7f5575d
Improve types for toThrowMatchers
Feb 18, 2019
a8ff374
Improve types for utils.ts
Feb 18, 2019
7f2aef4
Update diff file
Feb 18, 2019
602e34c
Add a typeguard for jasmineUtils
Feb 19, 2019
bf61f89
Update createToThrowErrorMatchingSnapshotMatcher typing
Feb 19, 2019
4ede592
Type getMessage and isPromise
Feb 19, 2019
72feb3e
Type case matcher call
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/expect/src/index.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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