forked from skovhus/jest-codemods
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sinon.js
495 lines (461 loc) · 15 KB
/
sinon.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
import { getRequireOrImportName, removeRequireAndImport } from '../utils/imports';
//import logger from '../utils/logger';
import finale from '../utils/finale';
// Todos: window functions should be global
const SINON = 'sinon';
const SINON_CALLED_WITH_METHODS = ['calledWith', 'notCalledWith'];
const TRUE_FALSE_MATCHERS = ['toBe', 'toBeTruthy', 'toBeFalsy'];
const SINON_CALL_COUNT_METHODS = [
'called',
'calledOnce',
'notCalled',
'calledTwice',
'calledThrice',
'callCount',
];
export default function expectJsTransfomer(fileInfo, api, options) {
const j = api.jscodeshift;
const ast = j(fileInfo.source);
const sinonImport = getRequireOrImportName(j, ast, SINON);
//const logWarning = (msg, node) => logger(fileInfo, msg, node);
if (!sinonImport) {
// No sinon require/import were found
return fileInfo.source;
}
removeRequireAndImport(j, ast, SINON);
[
transformSinonMock,
transformCallCountAssertions,
transformCalledWithAssertions,
transformSpyCreation,
transformStubCreation,
transformGetCallMethos,
].forEach(fn => {
fn(j, ast);
});
return finale(fileInfo, j, ast, options, sinonImport);
}
/**
* Transformations
*/
// sinon.mock() -> jest.fn()
function transformSinonMock(j, ast) {
ast
.find(j.CallExpression, {
callee: {
object: {
name: SINON,
},
property: {
name: 'mock',
},
},
})
.replaceWith(path => {
return createJestFn(j);
});
}
/**
* There is no direct equivalent for sinon.stub in Jest
* so use jest.spyOn and 'mockReturnValue'
* e.g. sinon.stub(obj, 'method1') -> jest.spyOn(obj, 'method1').mockReturnValue(undefined);
*/
function transformStubCreation(j, ast) {
ast
.find(j.ExpressionStatement, {
expression: {
type: 'CallExpression',
callee: isSinonStubCall,
},
})
.replaceWith(path => {
return j.expressionStatement(createJestSpyCall(j, path.value.expression));
});
/*
* Find the sinon variables that specify return values after the fact e.g.
* var stub2 = sinon.stub(obj, 'method4');
* stub2.returns('bye').
*/
ast
.find(j.VariableDeclarator, {
init: {
type: 'CallExpression',
callee: isSinonStubCall,
},
})
.replaceWith(path => {
ast
.find(j.CallExpression, {
callee: {
type: 'MemberExpression',
object: {
name: path.value.id.name,
},
},
})
.replaceWith(subPath => {
return j.callExpression(
j.memberExpression(
subPath.value.callee.object,
j.identifier('mockReturnValue')
),
subPath.value.arguments
);
});
return j.variableDeclarator(
path.value.id,
createJestSpyCall(j, path.value.init)
);
});
}
// spy.firstCall -> spy.mock.calls[0]
function transformGetCallMethos(j, ast) {
const getCallMethods = {
firstCall: 'firstCall',
secondCall: 'secondCall',
thirdCall: 'thirdCall',
lastCall: 'lastCall',
};
const methods = Object.keys(getCallMethods);
const MOCK_CALLS = 'mock.calls';
const createJestGetCall = (obj, callArg) => {
return j.memberExpression(
j.memberExpression(obj, j.identifier(MOCK_CALLS)),
callArg,
true
);
};
// remove `.args` from these type of sinon call methods
ast
.find(j.MemberExpression, {
object: {
type: 'MemberExpression',
property: {
name: name => methods.includes(name),
},
},
property: {
type: 'Identifier',
name: 'args',
},
})
.replaceWith(path => {
return path.value.object;
});
ast
.find(j.MemberExpression, {
object: {
type: 'CallExpression',
callee: {
type: 'MemberExpression',
property: {
name: 'getCall',
},
},
},
property: {
type: 'Identifier',
name: 'args',
},
})
.replaceWith(path => {
return path.value.object;
});
// now replace the call methods
ast
.find(j.MemberExpression, {
property: {
name: name => methods.includes(name),
},
})
.replaceWith(path => {
const obj = path.value.object;
switch (path.value.property.name) {
case getCallMethods.firstCall:
return createJestGetCall(obj, j.literal(0));
case getCallMethods.secondCall:
return createJestGetCall(obj, j.literal(1));
case getCallMethods.thirdCall:
return createJestGetCall(obj, j.literal(2));
default:
return createJestGetCall(
obj,
j.binaryExpression(
'-',
j.memberExpression(
j.memberExpression(obj, j.identifier(MOCK_CALLS)),
j.identifier('length')
),
j.literal(1)
)
);
}
});
ast
.find(j.CallExpression, {
callee: {
property: {
name: 'getCalls',
},
},
})
.replaceWith(path => {
return j.memberExpression(path.value.callee.object, j.identifier(MOCK_CALLS));
});
ast
.find(j.CallExpression, {
callee: {
property: {
name: 'getCall',
},
},
})
.replaceWith(path => {
return createJestGetCall(path.value.callee.object, path.value.arguments[0]);
});
}
// sinon.spy(object, 'method') -> jest.spyOn(object, 'method')
function transformSpyCreation(j, ast) {
ast
.find(j.CallExpression, {
callee: {
object: {
name: SINON,
},
property: {
name: 'spy',
},
},
})
.replaceWith(path => {
switch (path.value.arguments.length) {
case 0:
return createJestFn(j);
case 1:
return createJestFn(j, path.value.arguments);
case 2:
return j.callExpression(
j.identifier('jest.spyOn'),
path.value.arguments
);
default:
return path.value;
}
});
}
// expect(spy.calledWith(1, 2, 3)).toBe(true) -> expect(spy).toHaveBeenCalledWith(1, 2, 3);
function transformCalledWithAssertions(j, ast) {
ast
.find(j.ExpressionStatement, {
expression: {
callee: {
type: 'MemberExpression',
property: node => {
return TRUE_FALSE_MATCHERS.includes(node.name);
},
object: obj => {
return isExpectSinonCall(obj, SINON_CALLED_WITH_METHODS);
},
},
},
})
.replaceWith(path => {
const expectArg = getExpectArg(path.value.expression.callee.object);
const expectArgObject = expectArg.callee.object;
const expectArgSinonMethod = expectArg.callee.property.name;
let negation = isExpectNegation(path.value);
if (expectArgSinonMethod === 'notCalledWith') {
negation = negation ? false : true;
}
const createExpect = createExpectStatement.bind(
null,
j,
expectArgObject,
negation
);
switch (expectArgSinonMethod) {
case 'calledWith':
case 'notCalledWith':
return createExpect('toHaveBeenCalledWith', expectArg.arguments);
default:
path.value;
}
});
}
// expect(spy.called).toBe(true) -> expect(spy).toHaveBeenCalled()
function transformCallCountAssertions(j, ast) {
ast
.find(j.ExpressionStatement, {
expression: {
callee: {
type: 'MemberExpression',
property: node => {
return TRUE_FALSE_MATCHERS.includes(node.name);
},
object: obj => {
return isExpectSinonObject(obj, SINON_CALL_COUNT_METHODS);
},
},
},
})
.replaceWith(path => {
const expectArg = getExpectArg(path.value.expression.callee.object);
const expectArgObject = expectArg.object;
const expectArgSinonMethod = expectArg.property.name;
let negation = isExpectNegation(path.value);
if (expectArgSinonMethod === 'notCalled') {
negation = negation ? false : true;
}
const createExpect = createExpectStatement.bind(
null,
j,
expectArgObject,
negation
);
switch (expectArgSinonMethod) {
case 'called':
case 'calledOnce':
case 'notCalled':
return createExpect('toHaveBeenCalled');
case 'calledTwice':
return createExpect('toHaveBeenCalledTimes', [j.literal(2)]);
case 'calledThrice':
return createExpect('toHaveBeenCalledTimes', [j.literal(3)]);
default:
// callCount
return createExpect(
'toHaveBeenCalledTimes',
path.value.expression.arguments
);
}
});
}
/**
* Helper Functions
*/
function isSinonStubCall(callee) {
if (callee.object && callee.object.type === 'CallExpression') {
return isSinonStubCall(callee.object.callee);
}
if (
callee.type === 'MemberExpression' &&
callee.object.name === SINON &&
callee.property.name === 'stub'
) {
return true;
}
return false;
}
function isExpectSinonCall(obj, sinonMethods) {
if (obj.type === 'CallExpression' && obj.callee.name === 'expect') {
const args = obj.arguments;
if (args.length) {
return (
args[0].type === 'CallExpression' &&
args[0].callee.type === 'MemberExpression' &&
sinonMethods.includes(args[0].callee.property.name)
);
}
return false;
} else if (obj.type === 'MemberExpression') {
return isExpectSinonObject(obj.object, sinonMethods);
}
}
function isExpectSinonObject(obj, sinonMethods) {
if (obj.type === 'CallExpression' && obj.callee.name === 'expect') {
const args = obj.arguments;
if (args.length) {
return (
args[0].type === 'MemberExpression' &&
sinonMethods.includes(args[0].property.name)
);
}
return false;
} else if (obj.type === 'MemberExpression') {
return isExpectSinonObject(obj.object, sinonMethods);
}
}
function isExpectNegation(expectStatement) {
const propName = expectStatement.expression.callee.property.name;
const hasNot =
expectStatement.expression.callee.object.type === 'MemberExpression' &&
expectStatement.expression.callee.object.property.name === 'not';
const assertFalsy =
(propName === 'toBe' &&
expectStatement.expression.arguments[0].value === false) ||
propName === 'toBeFalsy';
if (hasNot && assertFalsy) {
return false;
}
return hasNot || assertFalsy;
}
function createJestSpyCall(j, callExpression) {
const callee = callExpression.callee;
const args = callExpression.arguments;
if (callee.object.type === 'CallExpression') {
if (callee.property && callee.property.name === 'returns') {
if (callee.object.arguments.length) {
return j.callExpression(
j.memberExpression(
j.callExpression(
j.identifier('jest.spyOn'),
callee.object.arguments
),
j.identifier('mockReturnValue')
),
args
);
} else {
return j.callExpression(
j.memberExpression(createJestFn(j), j.identifier('mockReturnValue')),
args
);
}
} else if (callee.property && callee.property.name === 'returnsArg') {
return j.callExpression(
j.memberExpression(
j.callExpression(j.identifier('jest.spyOn'), callee.object.arguments),
j.identifier('mockImplementation')
),
[
j.arrowFunctionExpression(
[j.restElement(j.identifier('args'))],
j.memberExpression(j.identifier('args'), args[0])
),
]
);
}
}
if (args.length) {
return j.callExpression(
j.memberExpression(
j.callExpression(j.identifier('jest.spyOn'), args),
j.identifier('mockReturnValue')
),
[j.identifier('undefined')]
);
} else {
return createJestFn(j);
}
}
function getExpectArg(obj) {
if (obj.type === 'MemberExpression') {
return getExpectArg(obj.object);
} else {
return obj.arguments[0];
}
}
function createExpectStatement(j, expectArg, negation, assertMethod, assertArgs) {
return j.expressionStatement(
j.callExpression(
j.memberExpression(
j.callExpression(j.identifier('expect'), [expectArg]),
j.identifier((negation ? 'not.' : '') + assertMethod)
),
assertArgs ? assertArgs : []
)
);
}
function createJestFn(j, args = []) {
return j.callExpression(j.identifier('jest.fn'), args);
}