Skip to content

Instantly share code, notes, and snippets.

@quetzaluz
Created October 9, 2018 16:04
Show Gist options
  • Save quetzaluz/07fa3b453edac9f22ae16c5bd4e82861 to your computer and use it in GitHub Desktop.
Save quetzaluz/07fa3b453edac9f22ae16c5bd4e82861 to your computer and use it in GitHub Desktop.
Method for quickly generating jest spy boilerplate given a list of stringified module dot class
function generateSpyFromMethod (mthdList) {
const top = []
const bottom = []
mthdList.forEach(function (mthd) {
const spl = mthd.split('.')
let first = spl[0]
let last = spl[1]
first = first[0].toLowerCase() + first.slice(1)
last = last[0].toUpperCase() + last.slice(1)
const spyVar = `${first}${last}Spy`
top.push(`let ${spyVar}`)
bottom.push(`
${spyVar} = spyOn(
${spl[0]},
'${spl[1]}'
)
`)
})
return (`
${top.join('\n')}
beforeEach(() => {
${bottom.join('\n')}
})
`)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment