Skip to content

Instantly share code, notes, and snippets.

@ideadapt
Last active May 22, 2016 21:44
Show Gist options
  • Save ideadapt/1d14e11841de3ede13a89d0d5e52c835 to your computer and use it in GitHub Desktop.
Save ideadapt/1d14e11841de3ede13a89d0d5e52c835 to your computer and use it in GitHub Desktop.

Revisions

  1. ideadapt revised this gist May 22, 2016. No changes.
  2. ideadapt revised this gist May 22, 2016. No changes.
  3. ideadapt created this gist May 22, 2016.
    59 changes: 59 additions & 0 deletions jasmine-done-callback-behaviour.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,59 @@
    describe('experiments with done', ()=>{

    describe('- correct usage -', ()=>{

    it('should fail because of async expect failure, and failure is assigned to correct spec', (done)=>{
    new Promise(function(res, rej){
    setTimeout(res, 1000)
    }).then(()=>{
    expect(1).toEqual(11)
    }).catch().then(done)
    })

    it('should wait for async done call', (done)=>{
    new Promise(function(res, rej){
    setTimeout(res, 1000)
    }).then(()=>{
    expect(4).toEqual(4)
    done()
    })
    })

    it('should wait for async done call, but fails meanwhile', (done)=>{
    new Promise(function(res, rej){
    setTimeout(res, 1000)
    }).then(()=>{
    expect(4).toEqual(44)
    done()
    })
    })

    it('should wait for async done call, which is at the end of the promise chain', (done)=>{
    new Promise(function(res, rej){
    setTimeout(res, 1000)
    }).then(()=>{
    expect(1).toEqual(1)
    }).catch().then(done)
    })
    })

    describe('- incorrect usage -', ()=>{

    it('should fail because of async expect failure, but wont since done not used', ()=>{
    new Promise(function(res, rej){
    // this expect throws an error, which jasmine is handling while already processing a succeeding spec.
    // hence the error will be assigned to that wrong spec.
    setTimeout(res, 1000)
    }).then(()=>{
    expect(2).toEqual(22)
    })
    })

    it('should fail with failed expect from previous spec', (done)=>{
    // most likely the failed expectation from previous spec will be detected and reported here.
    new Promise(function(res, rej){
    setTimeout(res, 1000)
    }).then(done)
    })
    })
    })