Created
May 14, 2019 20:45
-
-
Save jbmusso/ee7f633160bf238444d73a891463ab84 to your computer and use it in GitHub Desktop.
JavaScript: refactoring legacy functions with property-based testing and fast-check
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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