Skip to content

Commit

Permalink
Support jest 27
Browse files Browse the repository at this point in the history
  • Loading branch information
Roaders committed May 18, 2022
1 parent e8121f6 commit 6848356
Show file tree
Hide file tree
Showing 3 changed files with 16,959 additions and 6,237 deletions.
21 changes: 21 additions & 0 deletions main/mock/matchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,34 @@ export const matchers = {
wasCalledAtLeastOnce,
};

/* istanbul ignore next */
export function addMatchers() {
// jasmine.addMatchers must be called in a before function so this will sometimes throw an error
try {
jasmine.addMatchers(matchers);
} catch (e) {
// NOP
}

try {
(expect as unknown as jest.Expect).extend({
wasCalled: mapToJestCustomMatcher(wasCalled()),
wasCalledOnce: mapToJestCustomMatcher(wasCalledOnce()),
wasNotCalled: mapToJestCustomMatcher(wasNotCalled()),
wasCalledAtLeastOnce: mapToJestCustomMatcher(wasCalledAtLeastOnce()),
});
} catch (e) {
// NOP
}
}

/* istanbul ignore next */
function mapToJestCustomMatcher(matcher: ICustomMatcher): jest.CustomMatcher {
return (context: IFunctionVerifier<any, any>, received: any, ...actual: any[]) => {
const result = matcher.compare(context, received, ...actual);

return { pass: result.pass, message: () => result.message || 'failed' };
};
}

function wasCalled(): ICustomMatcher {
Expand Down
Loading

0 comments on commit 6848356

Please sign in to comment.