promise_aplus_test.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. const promise = require('../../lib/promise');
  19. const {enablePromiseManager} = require('../../lib/test/promise');
  20. describe('Promises/A+ Compliance Tests', function() {
  21. enablePromiseManager(() => {
  22. // The promise spec does not define behavior for unhandled rejections and
  23. // assumes they are effectively swallowed. This is not the case with our
  24. // implementation, so we have to disable error propagation to test that the
  25. // rest of our behavior is compliant.
  26. // We run the tests with a separate instance of the control flow to ensure
  27. // disablign error propagation does not impact other tests.
  28. var flow = new promise.ControlFlow();
  29. flow.setPropagateUnhandledRejections(false);
  30. // Skip the tests in 2.2.6.1/2. We are not compliant in these scenarios.
  31. var realDescribe = global.describe;
  32. global.describe = function(name, fn) {
  33. realDescribe(name, function() {
  34. var prefix = 'Promises/A+ Compliance Tests '
  35. + 'SELENIUM_PROMISE_MANAGER=true 2.2.6: '
  36. + '`then` may be called multiple times on the same promise.';
  37. var suffix = 'even when one handler is added inside another handler';
  38. if (this.fullTitle().startsWith(prefix)
  39. && this.fullTitle().endsWith(suffix)) {
  40. var realSpecify = global.specify;
  41. try {
  42. global.specify = function(name) {
  43. realSpecify(name);
  44. };
  45. fn();
  46. } finally {
  47. global.specify = realSpecify;
  48. }
  49. } else {
  50. fn();
  51. }
  52. });
  53. };
  54. require('promises-aplus-tests').mocha({
  55. resolved: function(value) {
  56. return new promise.Promise((fulfill) => fulfill(value), flow);
  57. },
  58. rejected: function(error) {
  59. return new promise.Promise((_, reject) => reject(error), flow);
  60. },
  61. deferred: function() {
  62. var d = new promise.Deferred(flow);
  63. return {
  64. resolve: d.fulfill,
  65. reject: d.reject,
  66. promise: d.promise
  67. };
  68. }
  69. });
  70. global.describe = realDescribe;
  71. });
  72. });