-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
/
jest_expect.js
64 lines (56 loc) · 1.98 KB
/
jest_expect.js
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
/**
* Copyright (c) 2014-present, Facebook, Inc. 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.
*
* @flow
*/
import type {RawMatcherFn} from 'types/Matchers';
import expect from 'expect';
import {
addSerializer,
toMatchSnapshot,
toMatchInlineSnapshot,
toThrowErrorMatchingSnapshot,
toThrowErrorMatchingInlineSnapshot,
} from 'jest-snapshot';
type JasmineMatcher = {
(matchersUtil: any, context: any): JasmineMatcher,
compare: () => RawMatcherFn,
negativeCompare: () => RawMatcherFn,
};
type JasmineMatchersObject = {[id: string]: JasmineMatcher};
export default (config: {expand: boolean}) => {
global.expect = expect;
expect.setState({expand: config.expand});
expect.extend({
toMatchInlineSnapshot,
toMatchSnapshot,
toThrowErrorMatchingInlineSnapshot,
toThrowErrorMatchingSnapshot,
});
(expect: Object).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(): 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, arguments)
: result.compare.apply(null, arguments);
};
});
const expect = global.expect;
expect.extend(jestMatchersObject);
};
};