Skip to content

Instantly share code, notes, and snippets.

@devenbansod
Last active March 8, 2018 08:18
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 devenbansod/03c5cff857661e076cbec72fcb2e7eb3 to your computer and use it in GitHub Desktop.
Save devenbansod/03c5cff857661e076cbec72fcb2e7eb3 to your computer and use it in GitHub Desktop.
Transformations to convert some basic assert.* functions to Jest expect
const functionsToBeTransformed = [ 'ok', 'equal', 'deepEqual', 'notEqual', 'throws', 'strictEqual' ];
const getParams = (propertyName, args) => {
switch (propertyName) {
case 'ok': return {
arguments1: [args[0]],
identifier2: 'toBeTruthy',
arguments2: []
};
case 'equal': return {
arguments1: [args[0]],
identifier2: 'toBe',
arguments2: [args[1]]
};
case 'deepEqual': return {
arguments1: [args[0]],
identifier2: 'toEqual',
arguments2: [args[1]]
};
case 'notEqual': return {
arguments1: [args[0]],
identifier2: 'not.toBe',
arguments2: [args[1]]
};
case 'throws': return {
arguments1: [args[0]],
identifier2: 'toThrow',
arguments2: [args[1]]
}
case 'strictEqual': return {
arguments1: [args[0]],
identifier2: 'toBe',
arguments2: [args[1]]
}
}
}
const getTransformedSource = (source, j, fnToTransform) => {
return j(source)
.find(j.CallExpression, {
callee: {
object: {
name: 'assert'
},
property: {
name: fnToTransform
}
}
})
.replaceWith((path) => {
let args = path.value.arguments;
let params = getParams(fnToTransform, args);
let a = j.expressionStatement(
j.callExpression(
j.memberExpression(
j.callExpression(
j.identifier('expect'), params.arguments1
),
j.identifier(params.identifier2)
), params.arguments2
)
);
return a;
}).
toSource();
}
export default function transformer(file, api) {
const j = api.jscodeshift;
let fileSource = file.source;
functionsToBeTransformed.forEach((fnToTransform) => {
fileSource = getTransformedSource(fileSource, j, fnToTransform);
});
return fileSource;
}
const functionsToBeTransformed = [
'isOk', 'isNotOk', 'equal',
'notEqual', 'strictEqual',
'notStrictEqual', 'deepEqual',
'notDeepEqual', 'isTrue',
'isFalse', 'isNotTrue', 'isNotFalse'
];
const getParams = (propertyName, args) => {
switch (propertyName) {
case 'isOk': return {
arguments1: [args[0]],
identifier2: 'toBeTruthy',
arguments2: []
};
case 'isNotOk': return {
arguments1: [args[0]],
identifier2: 'not.toBeTruthy',
arguments2: []
};
case 'equal': return {
arguments1: [args[0]],
identifier2: 'toBe',
arguments2: [args[1]]
};
case 'notEqual': return {
arguments1: [args[0]],
identifier2: 'not.toBe',
arguments2: [args[1]]
};
case 'deepEqual': return {
arguments1: [args[0]],
identifier2: 'toEqual',
arguments2: [args[1]]
};
case 'notDeepEqual': return {
arguments1: [args[0]],
identifier2: 'not.toEqual',
arguments2: [args[1]]
};
case 'throws': return {
arguments1: [args[0]],
identifier2: 'toThrow',
arguments2: [args[1]]
}
case 'strictEqual': return {
arguments1: [args[0]],
identifier2: 'toBe',
arguments2: [args[1]]
},
case 'notStrictEqual': return {
arguments1: [args[0]],
identifier2: 'not.toBe',
arguments2: [args[1]]
},
case 'isTrue': return {
arguments1: [args[0]],
identifier2: 'toBe',
arguments2: ['true']
},
case 'isNotTrue': return {
arguments1: [args[0]],
identifier2: 'not.toBe',
arguments2: ['true']
},
case 'isFalse': return {
arguments1: [args[0]],
identifier2: 'toBe',
arguments2: ['false']
},
case 'isNotFalse': return {
arguments1: [args[0]],
identifier2: 'not.toBe',
arguments2: ['false']
}
}
}
const getTransformedSource = (source, j, fnToTransform) => {
return j(source)
.find(j.CallExpression, {
callee: {
object: {
name: 'assert'
},
property: {
name: fnToTransform
}
}
})
.replaceWith((path) => {
let args = path.value.arguments;
let params = getParams(fnToTransform, args);
let a = j.expressionStatement(
j.callExpression(
j.memberExpression(
j.callExpression(
j.identifier('expect'), params.arguments1
),
j.identifier(params.identifier2)
), params.arguments2
)
);
return a;
}).
toSource();
}
export default function transformer(file, api) {
const j = api.jscodeshift;
let fileSource = file.source;
functionsToBeTransformed.forEach((fnToTransform) => {
fileSource = getTransformedSource(fileSource, j, fnToTransform);
});
return fileSource;
}
const functionsToBeTransformed = [ 'equal', 'eql' ];
const getParams = (propertyName, args1, args2) => {
switch (propertyName) {
case 'equal': return {
arguments1: args1,
identifier2: 'toBe',
arguments2: args2
};
case 'eql': return {
arguments1: args1,
identifier2: 'toEqual',
arguments2: args2
};
}
}
const getTransformedSource = (source, j, fnToTransform) => {
return j(source)
.find(j.CallExpression, {
callee: {
object: {
object: {
callee: {
name: 'expect'
}
},
},
property: {
name: fnToTransform
}
},
})
.replaceWith((path) => {
let args1 = path.value.callee.object.object.arguments;
let args2 = path.value.arguments;
let params = getParams(fnToTransform, args1, args2);
return j.expressionStatement(
j.callExpression(
j.memberExpression(
j.callExpression(
j.identifier('expect'), params.arguments1
),
j.identifier(params.identifier2)
), params.arguments2
)
);
}).
toSource();
}
export default function transformer(file, api) {
const j = api.jscodeshift;
let fileSource = file.source;
functionsToBeTransformed.forEach((fnToTransform) => {
fileSource = getTransformedSource(fileSource, j, fnToTransform);
});
return fileSource;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment