-
Notifications
You must be signed in to change notification settings - Fork 109
/
jest-arrow.js
50 lines (46 loc) · 1.08 KB
/
jest-arrow.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
/**
* Transforms all the normal functions into arrow functions as callback of
* jest globals such as describe, it...
*
* describe(function() {
* it('should work', function() {
* ...
* });
* });
*
* -->
*
* describe(() => {
* it('should work', () => {
* ...
* });
* });
*/
export default function transformer(file, api) {
const j = api.jscodeshift;
const functionsToTransform = [
'describe',
'beforeEach',
'afterEach',
'it',
'xit',
'test',
'xdescribe',
];
return j(file.source)
.find(j.ExpressionStatement)
.filter(path => {
return (
path.node.expression.type === 'CallExpression' &&
path.node.expression.callee.type === 'Identifier' &&
functionsToTransform.indexOf(path.node.expression.callee.name) !== -1
);
})
.forEach(path => {
var lastArg = path.node.expression.arguments.length - 1;
var fn = path.node.expression.arguments[lastArg];
path.node.expression.arguments[lastArg] =
j.arrowFunctionExpression(fn.params, fn.body);
})
.toSource();
}