promise.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Licensed to the Software Freedom Conservancy (SFC) under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. The SFC licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing,
  12. // software distributed under the License is distributed on an
  13. // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. // KIND, either express or implied. See the License for the
  15. // specific language governing permissions and limitations
  16. // under the License.
  17. 'use strict';
  18. /**
  19. * Configures a block of mocha tests, ensuring the promise manager is either
  20. * enabled or disabled for the tests' execution.
  21. * for their execution.
  22. */
  23. function withPromiseManager(enabled, fn) {
  24. describe(`SELENIUM_PROMISE_MANAGER=${enabled}`, function() {
  25. let saved;
  26. function changeState() {
  27. saved = process.env.SELENIUM_PROMISE_MANAGER;
  28. process.env.SELENIUM_PROMISE_MANAGER = enabled;
  29. }
  30. function restoreState() {
  31. if (saved === undefined) {
  32. delete process.env.SELENIUM_PROMISE_MANAGER;
  33. } else {
  34. process.env.SELENIUM_PROMISE_MANAGER = saved;
  35. }
  36. }
  37. before(changeState);
  38. after(restoreState);
  39. try {
  40. changeState();
  41. fn();
  42. } finally {
  43. restoreState();
  44. }
  45. });
  46. };
  47. /**
  48. * Defines a set of tests to run both with and without the promise manager
  49. * enabled.
  50. */
  51. exports.promiseManagerSuite = function(fn) {
  52. withPromiseManager(true, fn);
  53. withPromiseManager(false, fn);
  54. };
  55. /**
  56. * Ensures the promise manager is enabled when the provided tests run.
  57. */
  58. exports.enablePromiseManager = function(fn) {
  59. withPromiseManager(true, fn);
  60. };
  61. /**
  62. * Ensures the promise manager is disabled when the provided tests run.
  63. */
  64. exports.disablePromiseManager = function(fn) {
  65. withPromiseManager(false, fn);
  66. };