Skip to content

Instantly share code, notes, and snippets.

@JustinDFuller
Created October 13, 2018 18: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 JustinDFuller/ad6218cac91c543d9de50d30cd57340f to your computer and use it in GitHub Desktop.
Save JustinDFuller/ad6218cac91c543d9de50d30cd57340f to your computer and use it in GitHub Desktop.
Dependency Inversion
/* This is my file I'll be testing foo.js */
export default function ({ readFileAsync }) {
return {
readJsonFile (filePath) {
return readFileAsync(filePath).then(JSON.parse)
}
}
}
/* This is my test file foo.test.js */
import test from 'ava'
import foo from './foo'
test('foo with dependency inversion', function (t) {
t.plan(2)
const dependencies = {
readFileAsync(filePath) {
t.is(filePath, 'bar')
return Promise.resolve('{ success: true '})
}
}
const result = await foo(dependencies).readJsonFile('bar')
t.deepEqual(result, { success: true })
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment