Skip to content

Instantly share code, notes, and snippets.

@BartKrol
Last active September 8, 2016 08:17
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 BartKrol/ad019183186c27edc0bb22617a71925d to your computer and use it in GitHub Desktop.
Save BartKrol/ad019183186c27edc0bb22617a71925d to your computer and use it in GitHub Desktop.
DataLoader fails promise when Error is returned
import { expect } from 'chai'
import DataLoader from 'dataloader'
import sinon from 'sinon'
function createByIdLoader(loadFunc) {
return new DataLoader(
ids => Promise.all(
ids.map(loadFunc)
.map(promise => promise.catch(err => new Error(err)))
),
{
cache: false
}
)
}
describe('DataLoader', () => {
it('should return values when promises are fulfilled', () => { // Working
const value = 'success'
const stub = sinon.stub().returns(Promise.resolve(value))
const loader = createByIdLoader(stub)
return loader.loadMany([1, 2]).then(values => {
expect(values).to.deep.equal([value, value])
})
})
it('should return errors when promises are rejected', () => { // Failing
const value = 'fail'
const stub = sinon.stub().returns(Promise.reject(value))
const loader = createByIdLoader(stub)
return loader.loadMany([1, 2]).then(errors => {
expect(errors).to.deep.equal([new Error(value), new Error(value)])
})
.catch((err) => {
console.log('Should never be here')
throw err
})
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment