Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: capricorn86/happy-dom
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v15.2.0
Choose a base ref
...
head repository: capricorn86/happy-dom
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: a99364fa80f968da81dbb6ceb899ae15a8e5b902
Choose a head ref
  • 1 commit
  • 3 files changed
  • 2 contributors

Commits on Aug 28, 2024

  1. feat: [#1470] Adds support for the static method `AbortSignal.timeout…

    …()` (#1471)
    
    * feat: [#1470] Add AbortSignal.timeout() static method
    
    * chore: [#1468] As we use setTimeout() from Window, we cant use the fake timers from Vitest
    
    ---------
    
    Co-authored-by: David Ortner <david@ortner.se>
    ezzatron and capricorn86 authored Aug 28, 2024
    Copy the full SHA
    a99364f View commit details
1 change: 1 addition & 0 deletions packages/happy-dom/src/exception/DOMExceptionNameEnum.ts
Original file line number Diff line number Diff line change
@@ -14,6 +14,7 @@ enum DOMExceptionNameEnum {
invalidAccessError = 'InvalidAccessError',
unknownError = 'UnknownError',
abortError = 'AbortError',
timeoutError = 'TimeoutError',
encodingError = 'EncodingError',
uriMismatchError = 'URIMismatchError'
}
18 changes: 18 additions & 0 deletions packages/happy-dom/src/fetch/AbortSignal.ts
Original file line number Diff line number Diff line change
@@ -87,6 +87,24 @@ export default class AbortSignal extends EventTarget {
return signal;
}

/**
* Returns an AbortSignal that will automatically abort after a specified
* time.
*
* @param [time] Time in milliseconds.
* @returns AbortSignal instance.
*/
public static timeout(time: number): AbortSignal {
const window = this[PropertySymbol.window];
const signal = new this();
window.setTimeout(() => {
signal[PropertySymbol.abort](
new window.DOMException('signal timed out', DOMExceptionNameEnum.timeoutError)
);
}, time);
return signal;
}

/**
* Takes an iterable of abort signals and returns an AbortSignal that is
* aborted when any of the input iterable abort signals are aborted.
15 changes: 15 additions & 0 deletions packages/happy-dom/test/fetch/AbortSignal.test.ts
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@ import { describe, it, expect, beforeEach } from 'vitest';
import * as PropertySymbol from '../../src/PropertySymbol.js';
import BrowserWindow from '../../src/window/BrowserWindow.js';
import Window from '../../src/window/Window.js';
import DOMException from '../../src/exception/DOMException.js';

describe('AbortSignal', () => {
let window: BrowserWindow;
@@ -50,6 +51,20 @@ describe('AbortSignal', () => {
});
});

describe('AbortSignal.timeout()', () => {
it('Returns a new instance of AbortSignal that aborts with a "TimeoutError" after a timeout.', async () => {
const signal = window.AbortSignal.timeout(10);

expect(signal.aborted).toBe(false);

await new Promise((resolve) => setTimeout(resolve, 100));

expect(signal.aborted).toBe(true);
expect(signal.reason).toBeInstanceOf(DOMException);
expect(signal.reason?.name).toBe('TimeoutError');
});
});

describe('AbortSignal.any()', () => {
it('Returns a signal that is asynchronously aborted when one of the supplied signals is asynchronously aborted.', () => {
const signal1 = new window.AbortSignal();