0

I am testing promises which I get from the request-image-size library. Upon request from the library with an image URL I receive as a response of the image dimensions and an error if the image can`t be opened.

My test.js file, It case:

describe('Test images', () => {
  it('Should test all image sizes',async (done) => {

   const imagesResonses = await getAllImageResponses(imagesURLs)

      // if I console log imageResponses I get an array of promises, 
      // two of them must be rejected and throw an error
      // [
      //   Promise { <pending> },
      //   Promise { <pending> },
      //   Promise { <pending> },
      //   Promise { <pending> },
      //   Promise { <pending> },
      //   Promise { <pending> }
      // ]
      imagesResonses.map((image) => image.then((res) => {
        console.log(res)
        assert(res.height > 0, 'height must be greater than zero');
        done()
      }).catch((err) => {
        console.log(err)
        done(err)
      }))
  })

})

Test results: enter image description here

The problem:

I have successfully tested single promises before in mocha with using done(), but never multiple promises. I know I am not handling the promises the right way, but just dont know what else to try.

In this case, the tests are run 2 times and at the end I receive an error:

     done() called multiple times in test <Test links and images S
hould test all image sizes> of file C:path/test.js; in addition, don
e() received error: Error: Resolution method is overspecified. Specify a callback *or* return a Promise; not both.

Expectation: I want to run the test on each promise and do an assertion, and if the promise is rejected log out the error.

heyooo12
  • 139
  • 3
  • 16

1 Answers1

1

You can use a for..of loop along with await in order to execute your promises and make your assertions sequentially.

Haven't tested this but it should work:

for (const image of imagesResonses) {
  try {
    const res = await image
    assert(res.height > 0, 'height must be greater than zero');
  } catch(err) {
    console.log(err)
  }   
}

Also you can read more in this answer along with it's very interesting comments about why you can't achieve this using a forEach/map.

fgkolf
  • 910
  • 3
  • 15
  • Yes this kinda works, if I have no rejected promises and all are resolved all tests pass, but if it catches an error, it logs an error but the tests obviously pass. I want to tests to fail if I get an error ? How would I achieve this ? – heyooo12 Nov 26 '22 at 18:42
  • What is the expected behavior? The test should break in the first promise that gets rejected? – fgkolf Nov 26 '22 at 18:44
  • I solved it, I just put an assert that will fail in the catch block with an error message which I get from the res.error. – heyooo12 Nov 26 '22 at 18:51
  • You can also remove the `try..catch` totally. Probably you'll get the same result – fgkolf Nov 26 '22 at 18:55
  • If I remove try-catch, the tests don't pass which is correct, but I don't get additional feedback on which kind of error it has encountered that I get in the catch block. – heyooo12 Nov 26 '22 at 19:06
  • ok i see, maybe then call `done` with the error, as you did in your code – fgkolf Nov 26 '22 at 19:12