Skip to content

Instantly share code, notes, and snippets.

@rosswarren
Last active February 9, 2022 12:10
Show Gist options
  • Save rosswarren/ebbf6dd737275b8bbd43d533076c764e to your computer and use it in GitHub Desktop.
Save rosswarren/ebbf6dd737275b8bbd43d533076c764e to your computer and use it in GitHub Desktop.
Chai expect to assert
'use strict';
function buildExpectStatement(expectStatement) {
const properties = expectStatement.split('.').reverse();
const expectCall = {
type: 'CallExpression',
callee: {
type: 'Identifier',
name: 'expect'
}
}
let root = expectCall;
while(properties.length > 0) {
root = {
type: 'MemberExpression',
object: root,
property: {
type: 'Identifier',
name: properties.pop()
}
};
}
return root;
}
function buildExpectCall(expectStatement) {
return {
type: 'CallExpression',
callee: buildExpectStatement(expectStatement)
}
}
// Press ctrl+space for code completion
export default function transformer(file, api) {
const j = api.jscodeshift;
const root = j(file.source);
root.find(j.MemberExpression, buildExpectStatement('to.exist'))
.replaceWith(path => {
return j.callExpression(j.identifier('assert.exists'), [
path.value.object.object.arguments[0]
]);
});
root.find(j.CallExpression, buildExpectCall('to.eql'))
.replaceWith(path => {
return j.callExpression(j.identifier('assert.deepEqual'), path.value.callee.object.object.arguments.concat(path.value.arguments))
});
root.find(j.CallExpression, buildExpectCall('to.eq'))
.replaceWith(path => {
return j.callExpression(j.identifier('assert.equal'), path.value.callee.object.object.arguments.concat(path.value.arguments))
});
root.find(j.CallExpression, buildExpectCall('to.equal'))
.replaceWith(path => {
return j.callExpression(j.identifier('assert.equal'), path.value.callee.object.object.arguments.concat(path.value.arguments))
});
root.find(j.CallExpression, buildExpectCall('to.deep.eql'))
.replaceWith(path => {
return j.callExpression(j.identifier('assert.deepEqual'), path.value.callee.object.object.object.arguments.concat(path.value.arguments))
});
root.find(j.CallExpression, buildExpectCall('to.deep.equal'))
.replaceWith(path => {
return j.callExpression(j.identifier('assert.deepEqual'), path.value.callee.object.object.object.arguments.concat(path.value.arguments))
});
root.find(j.CallExpression, buildExpectCall('to.be.equal'))
.replaceWith(path => {
return j.callExpression(j.identifier('assert.equal'), path.value.callee.object.object.object.arguments.concat(path.value.arguments))
});
root.find(j.MemberExpression, buildExpectStatement('to.be.true'))
.replaceWith(path => {
return j.callExpression(j.identifier('assert.isTrue'), path.value.object.object.object.arguments)
});
root.find(j.MemberExpression, buildExpectStatement('to.be.false'))
.replaceWith(path => {
return j.callExpression(j.identifier('assert.isFalse'), path.value.object.object.object.arguments)
});
root.find(j.CallExpression, buildExpectCall('to.have.length'))
.replaceWith(path => {
return j.callExpression(j.identifier('assert.lengthOf'), path.value.callee.object.object.object.arguments.concat(path.value.arguments))
});
root.find(j.MemberExpression, buildExpectStatement('to.be.ok'))
.replaceWith(path => {
return j.callExpression(j.identifier('assert.isOk'), path.value.object.object.object.arguments)
});
root.find(j.MemberExpression, buildExpectStatement('to.be.okay'))
.replaceWith(path => {
return j.callExpression(j.identifier('assert.isOk'), path.value.object.object.object.arguments)
});
root.find(j.MemberExpression, buildExpectStatement('to.not.be.ok'))
.replaceWith(path => {
return j.callExpression(j.identifier('assert.isNotOk'), path.value.object.object.object.object.arguments)
});
root.find(j.CallExpression, buildExpectCall('to.contain'))
.replaceWith(path => {
return j.callExpression(j.identifier('assert.include'), path.value.callee.object.object.arguments.concat(path.value.arguments))
});
root.find(j.CallExpression, buildExpectCall('to.includes'))
.replaceWith(path => {
return j.callExpression(j.identifier('assert.include'), path.value.callee.object.object.arguments.concat(path.value.arguments))
});
root.find(j.CallExpression, buildExpectCall('to.include'))
.replaceWith(path => {
return j.callExpression(j.identifier('assert.include'), path.value.callee.object.object.arguments.concat(path.value.arguments))
});
root.find(j.CallExpression, buildExpectCall('contains'))
.replaceWith(path => {
return j.callExpression(j.identifier('assert.include'), path.value.callee.object.arguments.concat(path.value.arguments))
});
root.find(j.MemberExpression, buildExpectStatement('to.be.null'))
.replaceWith(path => {
return j.callExpression(j.identifier('assert.isNull'), path.value.object.object.object.arguments)
});
root.find(j.MemberExpression, buildExpectStatement('to.be.undefined'))
.replaceWith(path => {
return j.callExpression(j.identifier('assert.isUndefined'), path.value.object.object.object.arguments)
});
root.find(j.CallExpression, buildExpectCall('to.not.contain'))
.replaceWith(path => {
return j.callExpression(j.identifier('assert.notInclude'), path.value.callee.object.object.object.arguments.concat(path.value.arguments))
});
root.find(j.CallExpression, buildExpectCall('to.be.a'))
.replaceWith(path => {
return j.callExpression(j.identifier('assert.typeOf'), path.value.callee.object.object.object.arguments.concat(path.value.arguments))
});
root.find(j.MemberExpression, buildExpectStatement('to.have.been.calledOnce'))
.replaceWith(path => {
return j.callExpression(j.identifier('assert.isTrue'), [
j.memberExpression(path.value.object.object.object.object.arguments[0], j.identifier('calledOnce'))
]);
});
root.find(j.MemberExpression, buildExpectStatement('to.have.been.calledTwice'))
.replaceWith(path => {
return j.callExpression(j.identifier('assert.isTrue'), [
j.memberExpression(path.value.object.object.object.object.arguments[0], j.identifier('calledTwice'))
]);
});
root.find(j.MemberExpression, buildExpectStatement('to.have.been.called'))
.replaceWith(path => {
return j.callExpression(j.identifier('assert.isTrue'), [
j.memberExpression(path.value.object.object.object.object.arguments[0], j.identifier('called'))
]);
});
root.find(j.MemberExpression, buildExpectStatement('to.not.have.been.called'))
.replaceWith(path => {
return j.callExpression(j.identifier('assert.isFalse'), [
j.memberExpression(path.value.object.object.object.object.object.arguments[0], j.identifier('called'))
]);
});
root.find(j.CallExpression, buildExpectCall('to.eventually.become'))
.replaceWith(path => {
return j.callExpression(j.identifier('assert.becomes'), path.value.callee.object.object.object.arguments.concat(path.value.arguments))
});
root.find(j.CallExpression, buildExpectCall('to.eventually.be.rejectedWith'))
.replaceWith(path => {
return j.callExpression(j.identifier('assert.isRejected'), path.value.callee.object.object.object.object.arguments.concat(path.value.arguments))
});
root.find(j.MemberExpression, buildExpectStatement('to.eventually.be.fulfilled'))
.replaceWith(path => {
return j.callExpression(j.identifier('assert.isFulfilled'), path.value.object.object.object.object.arguments)
});
root.find(j.MemberExpression, buildExpectStatement('to.not.be.undefined'))
.replaceWith(path => {
return j.callExpression(j.identifier('assert.isDefined'), path.value.object.object.object.object.arguments)
});
root.find(j.CallExpression, buildExpectCall('to.throw'))
.replaceWith(path => {
return j.callExpression(j.identifier('assert.throws'), path.value.callee.object.object.arguments.concat(path.value.arguments))
});
root.find(j.CallExpression, buildExpectCall('to.match'))
.replaceWith(path => {
return j.callExpression(j.identifier('assert.match'), path.value.callee.object.object.arguments.concat(path.value.arguments))
});
return root.toSource({
wrapColumn: 400
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment