test-test-utils-async.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 { defer: async } = require('sdk/lang/functional');
  6. const { before, after } = require('sdk/test/utils');
  7. let AFTER_RUN = 0;
  8. let BEFORE_RUN = 0;
  9. /*
  10. * Tests are dependent on ordering, as the before and after functions
  11. * are called outside of each test, and sometimes checked in the next test
  12. * (like in the `after` tests)
  13. */
  14. exports.testABeforeAsync = function (assert, done) {
  15. assert.equal(BEFORE_RUN, 1, 'before function was called');
  16. BEFORE_RUN = 0;
  17. AFTER_RUN = 0;
  18. async(done)();
  19. };
  20. exports.testABeforeNameAsync = function (assert, done) {
  21. assert.equal(BEFORE_RUN, 2, 'before function was called with name');
  22. BEFORE_RUN = 0;
  23. AFTER_RUN = 0;
  24. async(done)();
  25. };
  26. exports.testAfterAsync = function (assert, done) {
  27. assert.equal(AFTER_RUN, 1, 'after function was called previously');
  28. BEFORE_RUN = 0;
  29. AFTER_RUN = 0;
  30. async(done)();
  31. };
  32. exports.testAfterNameAsync = function (assert, done) {
  33. assert.equal(AFTER_RUN, 2, 'after function was called with name');
  34. BEFORE_RUN = 0;
  35. AFTER_RUN = 0;
  36. async(done)();
  37. };
  38. exports.testSyncABefore = function (assert) {
  39. assert.equal(BEFORE_RUN, 1, 'before function was called for sync test');
  40. BEFORE_RUN = 0;
  41. AFTER_RUN = 0;
  42. };
  43. exports.testSyncAfter = function (assert) {
  44. assert.equal(AFTER_RUN, 1, 'after function was called for sync test');
  45. BEFORE_RUN = 0;
  46. AFTER_RUN = 0;
  47. };
  48. before(exports, (name, assert, done) => {
  49. if (name === 'testABeforeNameAsync')
  50. BEFORE_RUN = 2;
  51. else
  52. BEFORE_RUN = 1;
  53. assert.pass('assert passed into before function');
  54. async(done)();
  55. });
  56. after(exports, (name, assert, done) => {
  57. // testAfterName runs after testAfter, which is where this
  58. // check occurs in the assertation
  59. if (name === 'testAfterAsync')
  60. AFTER_RUN = 2;
  61. else
  62. AFTER_RUN = 1;
  63. assert.pass('assert passed into after function');
  64. async(done)();
  65. });
  66. require('sdk/test').run(exports);