Repository: puppeteer/puppeteer
File: test/src/page.spec.ts (TypeScript)

Code before:
          window.timeoutCounter = (window.timeoutCounter??0)+1;
          setTimeout(task, 0);
        };
        task();
      `);
      // Disable JavaScript, which should pause the timeout chain.
      await page.setJavaScriptEnabled(false);
      // Capture the counter's value after the timeout chain is paused.
      const timeoutCounter = await page.evaluate('window.timeoutCounter');
      // Wait for a microtask to complete to see if the timeout fires.
      await page.evaluate('Promise.resolve()');
      // Verify the counter has not changed, confirming that setTimeout was paused.
      expect(await page.evaluate('window.timeoutCounter')).toBe(timeoutCounter);
      // Re-enable JavaScript.
      await page.setJavaScriptEnabled(true);
      // Wait for another microtask.
      await page.evaluate('Promise.resolve()');
      // Verify the counter still has not changed, confirming that paused timeouts do not
      // resume upon re-enabling JavaScript.
      expect(await page.evaluate('window.timeoutCounter')).toBe(timeoutCounter);
    });
    it('then should not pause', async () => {
      const {page} = await getTestState();
      // Disable JavaScript execution on the page.
      await page.setJavaScriptEnabled(false);
      // This test verifies that native browser operations, like resolving a promise,
      // continue to work even when page scripts are disabled. The promise's .then()
      // callback should execute and return 42.
      expect(await page.evaluate('Promise.resolve().then(()=>42)')).toBe(42);
    });
  });

  describe('Page.setCacheEnabled', function () {
    it('should enable or disable the cache based on the state passed', async () => {
      const {page, server} = await getTestState();

      await page.goto(server.PREFIX + '/cached/one-style.html');
      const [cachedRequest] = await Promise.all([
        server.waitForRequest('/cached/one-style.html'),
        page.reload(),
      ]);
      // Rely on "if-modified-since" caching in our test server.
      expect(cachedRequest.headers['if-modified-since']).not.toBe(undefined);

      await page.setCacheEnabled(false);
      const [nonCachedRequest] = await Promise.all([
        server.waitForRequest('/cached/one-style.html'),
        page.reload(),
      ]);
      expect(nonCachedRequest.headers['if-modified-since']).toBe(undefined);
    });

Review comment (suggestion):
Let's refer to `.then` as a microtasks https://html.spec.whatwg.org/#microtask-queuing and for interval/SetTimeout `task` https://html.spec.whatwg.org/#queue-a-task

Code after:
      // Wait for another task. It should be long enough to avoid flakiness, as the
      // original `setInterval` will be throttled after several invocations.
      await page.evaluate(() => {
        return new Promise(resolve => {
          return setTimeout(resolve, 100);
        });
      });

      // Verify that the counter increased. This confirms that timers resumed when
      // JavaScript is re-enabled.
      expect(
        await page.evaluate(() => {
          return (globalThis as any).intervalCounter;
        }),
      ).toBeGreaterThan(intervalCounter);
    });
    it('setTimeout should stop', async () => {
      const {page} = await getTestState();

      // Set up a recursive setTimeout chain. The `task` function increments a counter and
      // immediately schedules itself to run again.
      await page.evaluate(() => {
        const task = () => {
          (globalThis as any).timeoutCounter =
            ((globalThis as any).timeoutCounter ?? 0) + 1;
          setTimeout(task, 0);
        };
        task();
      });

      // Disable JavaScript, which should pause the timeout chain.
      await page.setJavaScriptEnabled(false);

      // Capture the counter's value after the timeout chain is paused.
      const timeoutCounter = await page.evaluate(() => {
        return (globalThis as any).timeoutCounter;
      });

      // Wait for 100 ms. This gives the event loop with the task a
      // chance to run if it were not paused, which would have incremented the counter.
      await new Promise(resolve => {
        return setTimeout(resolve, 100);
      });

      // Verify the counter has not changed, confirming that setTimeout was paused.
      expect(
        await page.evaluate(() => {
          return (globalThis as any).timeoutCounter;
        }),
      ).toBe(timeoutCounter);