-
Notifications
You must be signed in to change notification settings - Fork 109
/
jest-11-update.js
90 lines (79 loc) · 2.13 KB
/
jest-11-update.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
const path = require('path');
module.exports = function(file, api, options = {}) {
if (
!file.path.includes(path.sep + '__tests__' + path.sep) &&
!file.path.includes(path.sep + '__mocks__' + path.sep)
) {
return null;
}
const j = api.jscodeshift;
const printOptions = options.printOptions || {quote: 'single'};
const root = j(file.source);
let mutations = 0;
const JEST_API = {
dontMock: 'unmock',
autoMockOff: 'disableAutomock',
autoMockOn: 'enableAutomock',
};
const isJestCall =
node => node.name == 'jest' || (
node.type == 'CallExpression' &&
node.callee.type == 'MemberExpression' &&
isJestCall(node.callee.object)
);
const updateAPIs = (apiMethods) =>
mutations += root
.find(j.CallExpression, {
callee: {
type: 'MemberExpression',
object: isJestCall,
property: {
name: name => apiMethods[name],
},
},
})
.forEach(p => {
const name = p.value.callee.property.name;
p.value.callee.property.name = apiMethods[name];
})
.size();
const JEST_MOCK_FNS = {
genMockFn: true,
genMockFunction: true,
};
const JEST_MOCK_IMPLEMENTATION = {
mockImpl: true,
mockImplementation: true,
};
const updateMockFns = () => {
mutations += root
.find(j.CallExpression, {
callee: {
type: 'MemberExpression',
object: {
name: 'jest',
},
property: {
name: name => JEST_MOCK_FNS[name],
},
},
})
.forEach(p => {
p.value.callee.property.name = 'fn';
const parent = p.parent.node;
const grandParent = p.parent.parent.node;
if (
parent.type == 'MemberExpression' &&
grandParent.type == 'CallExpression' &&
JEST_MOCK_IMPLEMENTATION[parent.property.name]
) {
p.value.arguments = grandParent.arguments;
j(p.parent.parent).replaceWith(p.value);
}
})
.size();
};
updateAPIs(JEST_API);
updateMockFns();
return mutations ? root.toSource(printOptions) : null;
};