diff options
Diffstat (limited to 'tests/unit/timers_test.ts')
-rw-r--r-- | tests/unit/timers_test.ts | 59 |
1 files changed, 56 insertions, 3 deletions
diff --git a/tests/unit/timers_test.ts b/tests/unit/timers_test.ts index 6e829c07f..679e57df7 100644 --- a/tests/unit/timers_test.ts +++ b/tests/unit/timers_test.ts @@ -4,6 +4,7 @@ import { assertEquals, assertNotEquals, delay, + DENO_FUTURE, execCode, unreachable, } from "./test_util.ts"; @@ -308,11 +309,63 @@ Deno.test(async function timeoutCallbackThis() { }; setTimeout(obj.foo, 1); await promise; - assertEquals(capturedThis, window); + if (!DENO_FUTURE) { + assertEquals(capturedThis, window); + } else { + assertEquals(capturedThis, globalThis); + } +}); + +Deno.test({ ignore: DENO_FUTURE }, async function timeoutBindThis() { + const thisCheckPassed = [null, undefined, globalThis, window]; + + const thisCheckFailed = [ + 0, + "", + true, + false, + {}, + [], + "foo", + () => {}, + Object.prototype, + ]; + + for (const thisArg of thisCheckPassed) { + const { promise, resolve } = Promise.withResolvers<void>(); + let hasThrown = 0; + try { + setTimeout.call(thisArg, () => resolve(), 1); + hasThrown = 1; + } catch (err) { + if (err instanceof TypeError) { + hasThrown = 2; + } else { + hasThrown = 3; + } + } + await promise; + assertEquals(hasThrown, 1); + } + + for (const thisArg of thisCheckFailed) { + let hasThrown = 0; + try { + setTimeout.call(thisArg, () => {}, 1); + hasThrown = 1; + } catch (err) { + if (err instanceof TypeError) { + hasThrown = 2; + } else { + hasThrown = 3; + } + } + assertEquals(hasThrown, 2); + } }); -Deno.test(async function timeoutBindThis() { - const thisCheckPassed = [null, undefined, window, globalThis]; +Deno.test({ ignore: !DENO_FUTURE }, async function timeoutBindThis() { + const thisCheckPassed = [null, undefined, globalThis]; const thisCheckFailed = [ 0, |