/
jestExpect.ts
66 lines (59 loc) · 2.04 KB
/
jestExpect.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/**
* 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.
*/
/* eslint-disable local/prefer-spread-eventually */
import {MatcherState, expect} from 'expect';
import {
addSerializer,
toMatchInlineSnapshot,
toMatchSnapshot,
toThrowErrorMatchingInlineSnapshot,
toThrowErrorMatchingSnapshot,
} from 'jest-snapshot';
import type {JasmineMatchersObject, RawMatcherFn} from './types';
export default function jestExpect(config: {expand: boolean}): void {
global.expect = expect;
expect.setState({expand: config.expand});
expect.extend({
toMatchInlineSnapshot,
toMatchSnapshot,
toThrowErrorMatchingInlineSnapshot,
toThrowErrorMatchingSnapshot,
});
expect.addSnapshotSerializer = addSerializer;
const jasmine = global.jasmine;
jasmine.anything = expect.anything;
jasmine.any = expect.any;
jasmine.objectContaining = expect.objectContaining;
jasmine.arrayContaining = expect.arrayContaining;
jasmine.stringMatching = expect.stringMatching;
jasmine.addMatchers = (jasmineMatchersObject: JasmineMatchersObject) => {
const jestMatchersObject = Object.create(null);
Object.keys(jasmineMatchersObject).forEach(name => {
jestMatchersObject[name] = function (
this: MatcherState,
...args: Array<unknown>
): RawMatcherFn {
// use "expect.extend" if you need to use equality testers (via this.equal)
const result = jasmineMatchersObject[name](null, null);
// if there is no 'negativeCompare', both should be handled by `compare`
const negativeCompare = result.negativeCompare || result.compare;
return this.isNot
? negativeCompare.apply(
null,
// @ts-expect-error
args,
)
: result.compare.apply(
null,
// @ts-expect-error
args,
);
};
});
expect.extend(jestMatchersObject);
};
}