test-test-loader.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. "use strict"
  5. const { LoaderWithHookedConsole } = require("sdk/test/loader");
  6. exports["test LoaderWithHookedConsole"] = function (assert) {
  7. let count = 0;
  8. function onMessage(type, message) {
  9. switch (count++) {
  10. case 0:
  11. assert.equal(type, "log", "got log type");
  12. assert.equal(message, "1st", "got log msg");
  13. break;
  14. case 1:
  15. assert.equal(type, "error", "got error type");
  16. assert.equal(message, "2nd", "got error msg");
  17. break;
  18. case 2:
  19. assert.equal(type, "warn", "got warn type");
  20. assert.equal(message, "3rd", "got warn msg");
  21. break;
  22. case 3:
  23. assert.equal(type, "info", "got info type");
  24. assert.equal(message, "4th", "got info msg");
  25. break;
  26. case 4:
  27. assert.equal(type, "debug", "got debug type");
  28. assert.equal(message, "5th", "got debug msg");
  29. break;
  30. case 5:
  31. assert.equal(type, "exception", "got exception type");
  32. assert.equal(message, "6th", "got exception msg");
  33. break;
  34. default:
  35. assert.fail("Got unexception message: " + i);
  36. }
  37. }
  38. let { loader, messages } = LoaderWithHookedConsole(module, onMessage);
  39. let console = loader.globals.console;
  40. console.log("1st");
  41. console.error("2nd");
  42. console.warn("3rd");
  43. console.info("4th");
  44. console.debug("5th");
  45. console.exception("6th");
  46. assert.equal(messages.length, 6, "Got all console messages");
  47. assert.deepEqual(messages[0], {type: "log", msg: "1st"}, "Got log");
  48. assert.deepEqual(messages[1], {type: "error", msg: "2nd"}, "Got error");
  49. assert.deepEqual(messages[2], {type: "warn", msg: "3rd"}, "Got warn");
  50. assert.deepEqual(messages[3], {type: "info", msg: "4th"}, "Got info");
  51. assert.deepEqual(messages[4], {type: "debug", msg: "5th"}, "Got debug");
  52. assert.deepEqual(messages[5], {type: "exception", msg: "6th"}, "Got exception");
  53. assert.equal(count, 6, "Called for all messages");
  54. };
  55. require("sdk/test").run(exports);