index_test.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 assert = require('assert');
  19. const promise = require('../..').promise;
  20. const {enablePromiseManager} = require('../../lib/test/promise');
  21. var test = require('../../testing');
  22. describe('Mocha Integration', function() {
  23. describe('beforeEach properly binds "this"', function() {
  24. beforeEach(function() { this.x = 1; });
  25. test.beforeEach(function() { this.x = 2; });
  26. it('', function() { assert.equal(this.x, 2); });
  27. });
  28. describe('afterEach properly binds "this"', function() {
  29. it('', function() { this.x = 1; });
  30. test.afterEach(function() { this.x = 2; });
  31. afterEach(function() { assert.equal(this.x, 2); });
  32. });
  33. describe('it properly binds "this"', function() {
  34. beforeEach(function() { this.x = 1; });
  35. test.it('', function() { this.x = 2; });
  36. afterEach(function() { assert.equal(this.x, 2); });
  37. });
  38. enablePromiseManager(function() {
  39. describe('timeout handling', function() {
  40. describe('it does not reset the control flow on a non-timeout', function() {
  41. var flowReset = false;
  42. beforeEach(function() {
  43. flowReset = false;
  44. test.controlFlow().once(promise.ControlFlow.EventType.RESET, onreset);
  45. });
  46. test.it('', function() {
  47. this.timeout(100);
  48. return promise.delayed(50);
  49. });
  50. afterEach(function() {
  51. assert.ok(!flowReset);
  52. test.controlFlow().removeListener(
  53. promise.ControlFlow.EventType.RESET, onreset);
  54. });
  55. function onreset() {
  56. flowReset = true;
  57. }
  58. });
  59. describe('it resets the control flow after a timeout' ,function() {
  60. var timeoutErr, flowReset;
  61. beforeEach(function() {
  62. flowReset = false;
  63. test.controlFlow().once(promise.ControlFlow.EventType.RESET, onreset);
  64. });
  65. test.it('', function() {
  66. var callback = this.runnable().callback;
  67. var test = this;
  68. this.runnable().callback = function(err) {
  69. timeoutErr = err;
  70. // Reset our timeout to 0 so Mocha does not fail the test.
  71. test.timeout(0);
  72. // When we invoke the real callback, do not pass along the error so
  73. // Mocha does not fail the test.
  74. return callback.call(this);
  75. };
  76. test.timeout(50);
  77. return promise.defer().promise;
  78. });
  79. afterEach(function() {
  80. return Promise.resolve().then(function() {
  81. test.controlFlow().removeListener(
  82. promise.ControlFlow.EventType.RESET, onreset);
  83. assert.ok(flowReset, 'control flow was not reset after a timeout');
  84. });
  85. });
  86. function onreset() {
  87. flowReset = true;
  88. }
  89. });
  90. });
  91. describe('async "done" support', function() {
  92. this.timeout(2*1000);
  93. var waited = false;
  94. var DELAY = 100; // ms enough to notice
  95. // Each test asynchronously sets waited to true, so clear/check waited
  96. // before/after:
  97. beforeEach(function() {
  98. waited = false;
  99. });
  100. afterEach(function() {
  101. assert.strictEqual(waited, true);
  102. });
  103. // --- First, vanilla mocha "it" should support the "done" callback correctly.
  104. // This 'it' should block until 'done' is invoked
  105. it('vanilla delayed', function(done) {
  106. setTimeout(function delayedVanillaTimeout() {
  107. waited = true;
  108. done();
  109. }, DELAY);
  110. });
  111. // --- Now with the webdriver wrappers for 'it' should support the "done" callback:
  112. test.it('delayed', function(done) {
  113. assert(done);
  114. assert.strictEqual(typeof done, 'function');
  115. setTimeout(function delayedTimeoutCallback() {
  116. waited = true;
  117. done();
  118. }, DELAY);
  119. });
  120. // --- And test that the webdriver wrapper for 'it' works with a returned promise, too:
  121. test.it('delayed by promise', function() {
  122. var defer = promise.defer();
  123. setTimeout(function delayedPromiseCallback() {
  124. waited = true;
  125. defer.fulfill('ignored');
  126. });
  127. return defer.promise;
  128. });
  129. });
  130. describe('ControlFlow and "done" work together', function() {
  131. var flow, order;
  132. before(function() {
  133. order = [];
  134. flow = test.controlFlow();
  135. flow.execute(function() { order.push(1); });
  136. });
  137. test.it('control flow updates and async done', function(done) {
  138. flow.execute(function() { order.push(2); });
  139. flow.execute(function() { order.push(3); }).then(function() {
  140. order.push(4);
  141. });
  142. done();
  143. });
  144. after(function() {
  145. assert.deepEqual([1, 2, 3, 4], order);
  146. });
  147. });
  148. });
  149. describe('generator support', function() {
  150. let arr;
  151. beforeEach(() => arr = []);
  152. afterEach(() => assert.deepEqual(arr, [0, 1, 2, 3]));
  153. test.it('sync generator', function* () {
  154. arr.push(yield arr.length);
  155. arr.push(yield arr.length);
  156. arr.push(yield arr.length);
  157. arr.push(yield arr.length);
  158. });
  159. test.it('async generator', function* () {
  160. arr.push(yield Promise.resolve(arr.length));
  161. arr.push(yield Promise.resolve(arr.length));
  162. arr.push(yield Promise.resolve(arr.length));
  163. arr.push(yield Promise.resolve(arr.length));
  164. });
  165. test.it('generator returns promise', function*() {
  166. arr.push(yield Promise.resolve(arr.length));
  167. arr.push(yield Promise.resolve(arr.length));
  168. arr.push(yield Promise.resolve(arr.length));
  169. setTimeout(_ => arr.push(arr.length), 10);
  170. return new Promise((resolve) => setTimeout(_ => resolve(), 25));
  171. });
  172. describe('generator runs with proper "this" context', () => {
  173. before(function() { this.values = [0, 1, 2, 3]; });
  174. test.it('', function*() {
  175. arr = this.values;
  176. });
  177. });
  178. it('generator function must not take a callback', function() {
  179. arr = [0, 1, 2, 3]; // For teardown hook.
  180. assert.throws(_ => {
  181. test.it('', function*(done){});
  182. }, TypeError);
  183. });
  184. });
  185. });