Skip to content

Instantly share code, notes, and snippets.

@pekala
Created October 11, 2016 13:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pekala/0c563cd4fd0ac965345c6dad71c4522d to your computer and use it in GitHub Desktop.
Save pekala/0c563cd4fd0ac965345c6dad71c4522d to your computer and use it in GitHub Desktop.
const getExpectExpression = (j, actualValue, expectedValue, methodName) => {
const innerCallExpression = j.callExpression(
j.identifier('expect'),
[actualValue]
);
const memberExpression = j.memberExpression(
innerCallExpression,
j.identifier(methodName)
);
return j.callExpression(
memberExpression,
expectedValue === undefined ? [] : [expectedValue]
);
};
module.exports = function(file, api) {
const j = api.jscodeshift;
const ast = j(file.source);
const comments = ast.find(j.Program).get('body', 0).node.comments;
ast.find(j.ImportDeclaration)
.filter(p => p.value.source.value === 'unexpected')
.forEach(p => p.prune());
ast.find(j.Program).get('body', 0).node.comments = comments;
ast.find(j.ReturnStatement)
.filter(p => {
const argument = p.value.argument;
if (!argument.callee) {
return false;
}
return argument.callee.name === 'expect' &&
argument.arguments.length === 3 &&
(argument.arguments[1].value === 'to be fulfilled with' || argument.arguments[1].value === 'to be rejected with');
})
.replaceWith(p => {
const expectExpression = getExpectExpression(j,
j.identifier('result'),
p.value.argument.arguments[2],
'toEqual'
);
const callback = j.arrowFunctionExpression([j.identifier('result')], expectExpression, true);
const memberEpression = j.memberExpression(
p.value.argument.arguments[0],
j.identifier(p.value.argument.arguments[1].value === 'to be fulfilled with' ? 'then' : 'catch')
);
return j.returnStatement(j.callExpression(memberEpression, [callback]));
});
ast.find(j.ReturnStatement)
.filter(p => {
const argument = p.value.argument;
if (!argument.callee || !argument.callee.object || !argument.callee.object.callee) {
return false;
}
return argument.callee.object.callee.name === 'expect' &&
argument.callee.property.name === 'then' &&
argument.callee.object.arguments.length === 3 &&
(argument.callee.object.arguments[1].value === 'to be fulfilled with' || argument.callee.object.arguments[1].value === 'to be rejected with');
})
.replaceWith(p => {
const expectExpression = j.expressionStatement(getExpectExpression(j,
j.identifier('result'),
p.value.argument.callee.object.arguments[2],
'toEqual'
));
const thenBody = p.value.argument.arguments[0].body.body || [j.expressionStatement(p.value.argument.arguments[0].body)];
const callback = j.arrowFunctionExpression([j.identifier('result')], j.blockStatement([expectExpression, ...thenBody]));
const memberEpression = j.memberExpression(
p.value.argument.callee.object.arguments[0],
j.identifier(p.value.argument.callee.object.arguments[1].value === 'to be fulfilled with' ? 'then' : 'catch')
);
return j.returnStatement(j.callExpression(memberEpression, [callback]));
});
ast.find(j.ReturnStatement)
.filter(p => {
const argument = p.value.argument;
if (!argument.callee) {
return false;
}
return argument.callee.name === 'expect' &&
argument.arguments.length === 2 &&
(argument.arguments[1].value === 'to be fulfilled' || argument.arguments[1].value === 'to be rejected');
})
.replaceWith(p => {
const callback = j.arrowFunctionExpression([], j.callExpression(j.identifier('expect'), []), true);
const memberEpression = j.memberExpression(
p.value.argument.arguments[0],
j.identifier(p.value.argument.arguments[1].value === 'to be fulfilled' ? 'then' : 'catch')
);
return j.returnStatement(j.callExpression(memberEpression, [callback]));
});
ast.find(j.ReturnStatement)
.filter(p => {
const argument = p.value.argument;
if (!argument.callee || !argument.callee.object || !argument.callee.object.callee) {
return false;
}
return argument.callee.object.callee.name === 'expect' &&
argument.callee.property.name === 'then' &&
argument.callee.object.arguments.length === 2 &&
(argument.callee.object.arguments[1].value === 'to be fulfilled' || argument.callee.object.arguments[1].value === 'to be rejected');
})
.replaceWith(p => {
const callback = j.arrowFunctionExpression([j.identifier('result')], j.blockStatement(p.value.argument.arguments[0].body.body || [j.expressionStatement(p.value.argument.arguments[0].body)]));
const memberEpression = j.memberExpression(
p.value.argument.callee.object.arguments[0],
j.identifier(p.value.argument.callee.object.arguments[1].value === 'to be fulfilled' ? 'then' : 'catch')
);
return j.returnStatement(j.callExpression(memberEpression, [callback]));
});
ast.find(j.CallExpression)
.filter(p => {
return p.value.callee.name === 'expect' &&
p.value.arguments.length === 2 &&
(p.value.arguments[1].value === 'to be truthy' || p.value.arguments[1].value === 'to be falsy');
})
.replaceWith(p => {
return getExpectExpression(j,
p.value.arguments[0],
undefined,
p.value.arguments[1].value === 'to be truthy' ? 'toBeTruthy' : 'toBeFalsy'
);
});
ast.find(j.CallExpression)
.filter(p => {
return p.value.callee.name === 'expect' &&
p.value.arguments.length === 3 &&
(p.value.arguments[1].value === 'to equal' || p.value.arguments[1].value === 'to be');
})
.replaceWith(p => {
return getExpectExpression(j,
p.value.arguments[0],
p.value.arguments[2],
p.value.arguments[1].value === 'to equal' ? 'toEqual' : 'toBe'
);
});
return ast.toSource({ quote: 'single' });
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment