test-test-utils-sync.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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) => {
  49. if (name === 'testABeforeNameAsync')
  50. BEFORE_RUN = 2;
  51. else
  52. BEFORE_RUN = 1;
  53. assert.pass('assert passed into before function');
  54. });
  55. after(exports, (name, assert) => {
  56. // testAfterName runs after testAfter, which is where this
  57. // check occurs in the assertation
  58. if (name === 'testAfterAsync')
  59. AFTER_RUN = 2;
  60. else
  61. AFTER_RUN = 1;
  62. assert.pass('assert passed into after function');
  63. });
  64. require('sdk/test').run(exports);