1

My code is failing when i run this test. I have tried

await expect(API().isFileAvailable).rejects.toEqual(Promise.reject(new Error('Bad input')));

But this doesn't work

File: async (a: string, b: string | undefined, c: string | undefined) => {
        if (!a&& !b) {
            return Promise.reject(new Error('Bad input'));
        }
}

enter image description here

Iliyas Shaik
  • 108
  • 5

1 Answers1

0

According to Jest's documentation, the rejects keyword should be used with toThrow() (which is an alias to toThrowError()).

You can directly use a string to match the error message, or a regex, or a Error object.

Your assertion should work with the following:

await expect(API().isFileAvailable).rejects.toThrow('Bad input');

Also, you should probably use throw in your code. Using Promise.reject() is less readable and the benefits of async/await is precisely to automatically reject when you throw an error.

File: async (a: string, b: string | undefined, c: string | undefined) => {
        if (!a&& !b) {
            // return Promise.reject(new Error('Bad input'));
            throw new Error('Bad input');
        }
}
Drarig29
  • 1,902
  • 1
  • 19
  • 42