tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

browser_jsterm_await.js (2694B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 // Test that top-level await expressions work as expected.
      5 
      6 "use strict";
      7 
      8 const TEST_URI =
      9  "data:text/html;charset=utf-8,<!DOCTYPE html>Web Console test top-level await";
     10 
     11 add_task(async function () {
     12  // Enable await mapping.
     13  await pushPref("devtools.debugger.features.map-await-expression", true);
     14 
     15  const hud = await openNewTabAndConsole(TEST_URI);
     16 
     17  info("Evaluate a top-level await expression");
     18  const simpleAwait = `await new Promise(r => setTimeout(() => r(["await1"]), 500))`;
     19  await executeAndWaitForResultMessage(hud, simpleAwait, `Array [ "await1" ]`);
     20 
     21  // Check that the resulting promise of the async iife is not displayed.
     22  const messages = hud.ui.outputNode.querySelectorAll(".message .message-body");
     23  const messagesText = Array.from(messages)
     24    .map(n => n.textContent)
     25    .join(" - ");
     26  is(
     27    messagesText,
     28    `${simpleAwait} - Array [ "await1" ]`,
     29    "The output contains the the expected messages"
     30  );
     31 
     32  // Check that the timestamp of the result is accurate
     33  const { visibleMessages, mutableMessagesById } = hud.ui.wrapper
     34    .getStore()
     35    .getState().messages;
     36  const [commandId, resultId] = visibleMessages;
     37  const delta =
     38    mutableMessagesById.get(resultId).timeStamp -
     39    mutableMessagesById.get(commandId).timeStamp;
     40  Assert.greaterOrEqual(
     41    delta,
     42    500,
     43    `The result has a timestamp at least 500ms (${delta}ms) older than the command`
     44  );
     45 
     46  info("Check that assigning the result of a top-level await expression works");
     47  await executeAndWaitForResultMessage(
     48    hud,
     49    `x = await new Promise(r => setTimeout(() => r("await2"), 500))`,
     50    `await2`
     51  );
     52 
     53  let message = await executeAndWaitForResultMessage(
     54    hud,
     55    `"-" + x + "-"`,
     56    `"-await2-"`
     57  );
     58  ok(message.node, "`x` was assigned as expected");
     59 
     60  info("Check that a logged promise is still displayed as a promise");
     61  message = await executeAndWaitForResultMessage(
     62    hud,
     63    `new Promise(r => setTimeout(() => r(1), 1000))`,
     64    `Promise {`
     65  );
     66  ok(message, "Promise are displayed as expected");
     67 
     68  info("Check that then getters aren't called twice");
     69  message = await executeAndWaitForResultMessage(
     70    hud,
     71    // It's important to keep the last statement of the expression as it covers the original issue.
     72    // We could execute another expression to get `object.called`, but since we get a preview
     73    // of the object with an accurate `called` value, this is enough.
     74    `
     75    var obj = {
     76      called: 0,
     77      get then(){
     78        this.called++
     79      }
     80    };
     81    await obj`,
     82    `Object { called: 1, then: Getter }`
     83  );
     84 });