Skip to content

Instantly share code, notes, and snippets.

@jbmusso
Created May 14, 2019 20:45
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 jbmusso/ee7f633160bf238444d73a891463ab84 to your computer and use it in GitHub Desktop.
Save jbmusso/ee7f633160bf238444d73a891463ab84 to your computer and use it in GitHub Desktop.
JavaScript: refactoring legacy functions with property-based testing and fast-check
const fc = require('fast-check');
const _ = require('lodash');
let selectNames = data => {
let names = [];
data.forEach(element => {
names.push(element.name);
});
return names;
};
function refactoredSelectNames(data) {
return data.map(({ name }) => name);
}
function areEqualFunctions(func1, func2, ...args) {
return _.isEqual(func1(...args), func2(...args));
}
describe('selectNames', () => {
test('both functions are equal', () => {
const areEqualFunctionsProperty = _.partial(
areEqualFunctions,
selectNames,
refactoredSelectNames
);
const domain = fc.array(fc.record({ name: fc.anything() }));
fc.assert(fc.property(domain, areEqualFunctionsProperty), { verbose: true });
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment