Skip to content

Commit

Permalink
moar
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Oct 5, 2021
1 parent bae77ec commit 1349cad
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 17 deletions.
10 changes: 6 additions & 4 deletions packages/expect/src/asymmetricMatchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,22 @@ const utils = Object.freeze({
subsetEquality,
});

export abstract class AsymmetricMatcher<T>
implements AsymmetricMatcherInterface
export abstract class AsymmetricMatcher<
T,
State extends MatcherState = MatcherState,
> implements AsymmetricMatcherInterface
{
$$typeof = Symbol.for('jest.asymmetricMatcher');

constructor(protected sample: T, protected inverse = false) {}

protected getMatcherContext(): MatcherState {
protected getMatcherContext(): State {
return {
...getState(),
equals,
isNot: this.inverse,
utils,
};
} as State;
}

abstract asymmetricMatch(other: unknown): boolean;
Expand Down
17 changes: 11 additions & 6 deletions packages/expect/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,8 +354,9 @@ const makeThrowingMatcher = (
}
};

expect.extend = (matchers: MatchersObject): void =>
setMatchers(matchers, false, expect);
expect.extend = <T extends JestMatcherState = JestMatcherState>(
matchers: MatchersObject<T>,
): void => setMatchers(matchers, false, expect);

expect.anything = anything;
expect.any = any;
Expand Down Expand Up @@ -396,8 +397,10 @@ function assertions(expected: number) {
Error.captureStackTrace(error, assertions);
}

getState().expectedAssertionsNumber = expected;
getState().expectedAssertionsNumberError = error;
setState({
expectedAssertionsNumber: expected,
expectedAssertionsNumberError: error,
});
}
function hasAssertions(...args: Array<any>) {
const error = new Error();
Expand All @@ -406,8 +409,10 @@ function hasAssertions(...args: Array<any>) {
}

matcherUtils.ensureNoExpected(args[0], '.hasAssertions');
getState().isExpectingAssertions = true;
getState().isExpectingAssertionsError = error;
setState({
isExpectingAssertions: true,
isExpectingAssertionsError: error,
});
}

// add default jest matchers
Expand Down
18 changes: 11 additions & 7 deletions packages/expect/src/jestMatchersObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,21 @@ if (!global.hasOwnProperty(JEST_MATCHERS_OBJECT)) {
});
}

export const getState = (): MatcherState =>
export const getState = <State extends MatcherState = MatcherState>(): State =>
(global as any)[JEST_MATCHERS_OBJECT].state;

export const setState = (state: Partial<MatcherState>): void => {
export const setState = <State extends MatcherState = MatcherState>(
state: Partial<State>,
): void => {
Object.assign((global as any)[JEST_MATCHERS_OBJECT].state, state);
};

export const getMatchers = (): MatchersObject =>
(global as any)[JEST_MATCHERS_OBJECT].matchers;
export const getMatchers = <
State extends MatcherState = MatcherState,
>(): MatchersObject<State> => (global as any)[JEST_MATCHERS_OBJECT].matchers;

export const setMatchers = (
matchers: MatchersObject,
export const setMatchers = <State extends MatcherState = MatcherState>(
matchers: MatchersObject<State>,
isInternal: boolean,
expect: Expect,
): void => {
Expand All @@ -62,7 +65,8 @@ export const setMatchers = (
// expect is defined

class CustomMatcher extends AsymmetricMatcher<
[unknown, ...Array<unknown>]
[unknown, ...Array<unknown>],
State
> {
constructor(
inverse: boolean = false,
Expand Down

0 comments on commit 1349cad

Please sign in to comment.