Skip to content

Commit

Permalink
Only test AbortController if defined
Browse files Browse the repository at this point in the history
  • Loading branch information
IanVS committed Mar 11, 2022
1 parent 4737d13 commit 255accd
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions tests/api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,19 +119,23 @@ describe('Mocking aborts', () => {
});

it('throws when passed an already aborted abort signal in the request init', () => {
const c = new AbortController();
c.abort();
expect(() => fetch('/', { signal: c.signal })).toThrow(expect.any(DOMException));
if (typeof AbortController !== 'undefined') {
const c = new AbortController();
c.abort();
expect(() => fetch('/', { signal: c.signal })).toThrow(expect.any(DOMException));
}
});

it('rejects when aborted before resolved', async () => {
const c = new AbortController();
fetch.mockResponse(async () => {
vi.advanceTimersByTime(60);
return '';
});
setTimeout(() => c.abort(), 50);
await expect(fetch('/', { signal: c.signal })).rejects.toThrow(expect.any(DOMException));
if (typeof AbortController !== 'undefined') {
const c = new AbortController();
fetch.mockResponse(async () => {
vi.advanceTimersByTime(60);
return '';
});
setTimeout(() => c.abort(), 50);
await expect(fetch('/', { signal: c.signal })).rejects.toThrow(expect.any(DOMException));
}
});
});

Expand Down

0 comments on commit 255accd

Please sign in to comment.