<commit_before>"use strict";
const jsdom = require("../..").jsdom;
const createVirtualConsole = require("../..").createVirtualConsole;
const toFileUrl = require("../util").toFileUrl;
const todo = require("../util").todo;

exports["onerror catches exceptions thrown in addEventListener event handlers"] = function (t) {
  const doc = jsdom("", { url: "http://example.com" });

  const error = new Error("oh no!");
  doc.body.addEventListener("click", function () {
    throw error;
  });

  doc.defaultView.addEventListener("error", function (event) {
    t.equal(event.message, "oh no!");

    todo(t, function (t) { // TODO url parser
      t.ok(event.filename === toFileUrl(__filename), "filename equality");
    });

    t.ok(event.lineno > 0);
    t.ok(event.colno > 0);
    t.equal(event.error, error);
    t.done();
  });

  doc.body.click();
};

exports["onerror property catches exceptions thrown in addEventListener event handlers"] = function (t) {
  const doc = jsdom("", { url: "http://example.com" });

  const error = new Error("oh no!");
  doc.body.addEventListener("click", function () {
    throw error;
  });

  doc.defaultView.onerror = function (message, filename, lineno, colno, error) {
    t.equal(message, "oh no!");

    todo(t, function (t) { // TODO url parser
      t.ok(filename === toFileUrl(__filename), "filename equality");
    });

    t.ok(lineno > 0);
    t.ok(colno > 0);
    t.equal(error, error);
    t.done();
  };

  doc.body.click();
};

exports["onerror catches exceptions thrown in addEventListener event handlers (multiline message)"] = function (t) {
  const doc = jsdom("", { url: "http://example.com" });

  const error = new Error("oh\nno\n!");
  doc.body.addEventListener("click", function () {
    throw error;
  });

  doc.defaultView.addEventListener("error", function (event) {
    todo(t, function (t) { // TODO url parser
      t.ok(event.filename === toFileUrl(__filename), "filename equality");
    });

    t.ok(event.lineno > 0);
    t.ok(event.colno > 0);
    t.equal(event.error, error);
    t.done();
  });

  doc.body.click();
};

exports["onerror catches exceptions thrown in inline event handlers"] = function (t) {
  const doc = jsdom(`<body onclick="throw new Error('oh no!')"></body>`, { url: "http://example.com" });

  doc.defaultView.addEventListener("error", function (event) {
    t.equal(event.message, "oh no!");
    t.equal(event.filename, "http://example.com");
    t.ok(event.lineno > 0, "lineno set");
    t.ok(event.colno > 0, "colno set");
    t.ok(event.error);
    t.done();
  });

  doc.body.click();
};

exports["onerror catches exceptions thrown in inline event handler properties"] = function (t) {
  const doc = jsdom("", { url: "http://example.com" });

  doc.body.onclick = function () {
    throw new Error("oh no!");
  };

  doc.defaultView.addEventListener("error", function (event) {
    t.equal(event.message, "oh no!", "message equality");

    todo(t, function (t) { // TODO url parser
      t.ok(event.filename === toFileUrl(__filename), "filename equality");
    });

    t.ok(event.lineno > 0, "lineno set");
    t.ok(event.colno > 0, "colno set");
    t.ok(event.error);
    t.done();
  });

  doc.body.click();
};

exports["onerror catches exceptions thrown in sync script execution"] = function (t) {
  const doc = jsdom("", { url: "http://example.com" });

  doc.defaultView.addEventListener("error", function (event) {
    t.equal(event.message, "oh no!", "message equality");
    t.equal(event.filename, "http://example.com");
    t.ok(event.lineno > 0, "lineno set");
    t.ok(event.colno > 0, "colno set");
    t.ok(event.error);
    t.done();
  });

  doc.body.innerHTML = `<script>throw new Error("oh no!");</script>`;
};

exports["onerror set during parsing catches exceptions thrown in sync script execution during parsing"] = function (t) {
  const doc = jsdom(`<script>
    onerror = function (message, filename, lineno, colno, error) {
      window.onerrorMessage = message;
      window.onerrorFilename = filename;
      window.onerrorLineno = lineno;
      window.onerrorColno = colno;
      window.onerrorError = error;
    };
    throw new Error("oh no!");
  </script>`, { url: "http://example.com" });

  t.equal(doc.defaultView.onerrorMessage, "oh no!", "message equality");
  t.equal(doc.defaultView.onerrorFilename, "http://example.com");
  t.ok(doc.defaultView.onerrorLineno > 0, "lineno set");
  t.ok(doc.defaultView.onerrorColno > 0, "colno set");
  t.ok(doc.defaultView.onerrorError);
  t.done();
};

exports["unhandled Errors thrown in sync script excecution during parsing go to the virtual console"] = function (t) {
  const virtualConsole = createVirtualConsole();
  virtualConsole.on("jsdomError", function (error) {
    t.ok(error instanceof Error);
    t.ok(error.message === "Uncaught [TypeError: oh no!]");
    t.equal(error.detail.constructor.name, "TypeError");
    t.done();
  });

  jsdom(`<script>throw new TypeError("oh no!")</script>`, { virtualConsole });
};

exports["unhandled non-Error exceptions thrown in sync script excecution during parsing go to the virtual console"] =
    function (t) {
  const virtualConsole = createVirtualConsole();
  virtualConsole.on("jsdomError", function (error) {
    t.ok(error instanceof Error);
    t.equal(error.message, "Uncaught {}");
    t.equal(typeof error.detail, "object");
    t.notEqual(error.detail, null);
    t.done();
  });

  jsdom(`<script>throw {}</script>`, { virtualConsole });
};

exports["unhandled exceptions thrown in inline event handlers go to the virtual console"] = function (t) {
  const virtualConsole = createVirtualConsole();
  virtualConsole.on("jsdomError", function (error) {
    t.ok(error instanceof Error);
    t.equal(error.message, "Uncaught [Error: oh no!]");
    t.equal(error.detail.constructor.name, "Error");
    t.done();
  });

  const doc = jsdom(`<body onclick="throw new Error('oh no!')"></body>`, { virtualConsole });

  doc.body.click();
};

exports["adding an onerror handler does not prevent errors from going to the virtual console"] = function (t) {
  const virtualConsole = createVirtualConsole();
  virtualConsole.on("jsdomError", function (error) {
    t.ok(error instanceof Error);<commit_msg>Update on-error.js test for io.js v3<commit_after>"use strict";
const jsdom = require("../..").jsdom;
const createVirtualConsole = require("../..").createVirtualConsole;
const toFileUrl = require("../util").toFileUrl;
const todo = require("../util").todo;

exports["onerror catches exceptions thrown in addEventListener event handlers"] = function (t) {
  const doc = jsdom("", { url: "http://example.com" });

  const error = new Error("oh no!");
  doc.body.addEventListener("click", function () {
    throw error;
  });

  doc.defaultView.addEventListener("error", function (event) {
    t.equal(event.message, "oh no!");

    todo(t, function (t) { // TODO url parser
      t.ok(event.filename === toFileUrl(__filename), "filename equality");
    });

    t.ok(event.lineno > 0);
    t.ok(event.colno > 0);
    t.equal(event.error, error);
    t.done();
  });

  doc.body.click();
};

exports["onerror property catches exceptions thrown in addEventListener event handlers"] = function (t) {
  const doc = jsdom("", { url: "http://example.com" });

  const error = new Error("oh no!");
  doc.body.addEventListener("click", function () {
    throw error;
  });

  doc.defaultView.onerror = function (message, filename, lineno, colno, error) {
    t.equal(message, "oh no!");

    todo(t, function (t) { // TODO url parser
      t.ok(filename === toFileUrl(__filename), "filename equality");
    });

    t.ok(lineno > 0);
    t.ok(colno > 0);
    t.equal(error, error);
    t.done();
  };

  doc.body.click();
};

exports["onerror catches exceptions thrown in addEventListener event handlers (multiline message)"] = function (t) {
  const doc = jsdom("", { url: "http://example.com" });

  const error = new Error("oh\nno\n!");
  doc.body.addEventListener("click", function () {
    throw error;
  });

  doc.defaultView.addEventListener("error", function (event) {
    todo(t, function (t) { // TODO url parser
      t.ok(event.filename === toFileUrl(__filename), "filename equality");
    });

    t.ok(event.lineno > 0);
    t.ok(event.colno > 0);
    t.equal(event.error, error);
    t.done();
  });

  doc.body.click();
};

exports["onerror catches exceptions thrown in inline event handlers"] = function (t) {
  const doc = jsdom(`<body onclick="throw new Error('oh no!')"></body>`, { url: "http://example.com" });

  doc.defaultView.addEventListener("error", function (event) {
    t.equal(event.message, "oh no!");
    t.equal(event.filename, "http://example.com");
    t.ok(event.lineno > 0, "lineno set");
    t.ok(event.colno > 0, "colno set");
    t.ok(event.error);
    t.done();
  });

  doc.body.click();
};

exports["onerror catches exceptions thrown in inline event handler properties"] = function (t) {
  const doc = jsdom("", { url: "http://example.com" });

  doc.body.onclick = function () {
    throw new Error("oh no!");
  };

  doc.defaultView.addEventListener("error", function (event) {
    t.equal(event.message, "oh no!", "message equality");

    todo(t, function (t) { // TODO url parser
      t.ok(event.filename === toFileUrl(__filename), "filename equality");
    });

    t.ok(event.lineno > 0, "lineno set");
    t.ok(event.colno > 0, "colno set");
    t.ok(event.error);
    t.done();
  });

  doc.body.click();
};

exports["onerror catches exceptions thrown in sync script execution"] = function (t) {
  const doc = jsdom("", { url: "http://example.com" });

  doc.defaultView.addEventListener("error", function (event) {
    t.equal(event.message, "oh no!", "message equality");
    t.equal(event.filename, "http://example.com");
    t.ok(event.lineno > 0, "lineno set");
    t.ok(event.colno > 0, "colno set");
    t.ok(event.error);
    t.done();
  });

  doc.body.innerHTML = `<script>throw new Error("oh no!");</script>`;
};

exports["onerror set during parsing catches exceptions thrown in sync script execution during parsing"] = function (t) {
  const doc = jsdom(`<script>
    onerror = function (message, filename, lineno, colno, error) {
      window.onerrorMessage = message;
      window.onerrorFilename = filename;
      window.onerrorLineno = lineno;
      window.onerrorColno = colno;
      window.onerrorError = error;
    };
    throw new Error("oh no!");
  </script>`, { url: "http://example.com" });

  t.equal(doc.defaultView.onerrorMessage, "oh no!", "message equality");
  t.equal(doc.defaultView.onerrorFilename, "http://example.com");
  t.ok(doc.defaultView.onerrorLineno > 0, "lineno set");
  t.ok(doc.defaultView.onerrorColno > 0, "colno set");
  t.ok(doc.defaultView.onerrorError);
  t.done();
};

exports["unhandled Errors thrown in sync script excecution during parsing go to the virtual console"] = function (t) {
  const virtualConsole = createVirtualConsole();
  virtualConsole.on("jsdomError", function (error) {
    t.ok(error instanceof Error);
    t.ok(error.message === "Uncaught [TypeError: oh no!]");
    t.equal(error.detail.constructor.name, "TypeError");
    t.done();
  });

  jsdom(`<script>throw new TypeError("oh no!")</script>`, { virtualConsole });
};

exports["unhandled non-Error exceptions thrown in sync script excecution during parsing go to the virtual console"] =
    function (t) {
  const virtualConsole = createVirtualConsole();
  virtualConsole.on("jsdomError", function (error) {
    t.ok(error instanceof Error);
    t.ok(error.message === "Uncaught Object {}" || error.message === "Uncaught {}"); // io.js v3 introduces the prefix
    t.equal(typeof error.detail, "object");
    t.notEqual(error.detail, null);
    t.done();
  });

  jsdom(`<script>throw {}</script>`, { virtualConsole });
};

exports["unhandled exceptions thrown in inline event handlers go to the virtual console"] = function (t) {
  const virtualConsole = createVirtualConsole();
  virtualConsole.on("jsdomError", function (error) {
    t.ok(error instanceof Error);
    t.equal(error.message, "Uncaught [Error: oh no!]");
    t.equal(error.detail.constructor.name, "Error");
    t.done();
  });

  const doc = jsdom(`<body onclick="throw new Error('oh no!')"></body>`, { virtualConsole });

  doc.body.click();
};

exports["adding an onerror handler does not prevent errors from going to the virtual console"] = function (t) {
  const virtualConsole = createVirtualConsole();
  virtualConsole.on("jsdomError", function (error) {
    t.ok(error instanceof Error);
{'fraction_non_alphanumeric': 0.1751929349988066, 'fraction_numerical': 0.0023868247275041768, 'mean_word_length': 3.8013750954927428, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4911688', 'n_tokens_mistral': 4059, 'n_tokens_neox': 3937, 'n_words': 990}