jasmine_async.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.install = install;
  6. var _isGeneratorFn = require('is-generator-fn');
  7. var _isGeneratorFn2 = _interopRequireDefault(_isGeneratorFn);
  8. var _co = require('co');
  9. var _co2 = _interopRequireDefault(_co);
  10. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  11. function isPromise(obj) {
  12. return obj && typeof obj.then === 'function';
  13. } /**
  14. * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
  15. *
  16. * This source code is licensed under the MIT license found in the
  17. * LICENSE file in the root directory of this source tree.
  18. *
  19. *
  20. */
  21. /**
  22. * This module adds ability to test async promise code with jasmine by
  23. * returning a promise from `it/test` and `before/afterEach/All` blocks.
  24. */
  25. function promisifyLifeCycleFunction(originalFn, env) {
  26. return function (fn, timeout) {
  27. if (!fn) {
  28. return originalFn.call(env);
  29. }
  30. const hasDoneCallback = fn.length > 0;
  31. if (hasDoneCallback) {
  32. // Jasmine will handle it
  33. return originalFn.call(env, fn, timeout);
  34. }
  35. // We make *all* functions async and run `done` right away if they
  36. // didn't return a promise.
  37. const asyncFn = function (done) {
  38. const wrappedFn = (0, _isGeneratorFn2.default)(fn) ? _co2.default.wrap(fn) : fn;
  39. const returnValue = wrappedFn.call({});
  40. if (isPromise(returnValue)) {
  41. returnValue.then(done.bind(null, null), done.fail);
  42. } else {
  43. done();
  44. }
  45. };
  46. return originalFn.call(env, asyncFn, timeout);
  47. };
  48. }
  49. // Similar to promisifyLifeCycleFunction but throws an error
  50. // when the return value is neither a Promise nor `undefined`
  51. function promisifyIt(originalFn, env) {
  52. return function (specName, fn, timeout) {
  53. if (!fn) {
  54. const spec = originalFn.call(env, specName);
  55. spec.pend('not implemented');
  56. return spec;
  57. }
  58. const hasDoneCallback = fn.length > 0;
  59. if (hasDoneCallback) {
  60. return originalFn.call(env, specName, fn, timeout);
  61. }
  62. const asyncFn = function (done) {
  63. const wrappedFn = (0, _isGeneratorFn2.default)(fn) ? _co2.default.wrap(fn) : fn;
  64. const returnValue = wrappedFn.call({});
  65. if (isPromise(returnValue)) {
  66. returnValue.then(done.bind(null, null), done.fail);
  67. } else if (returnValue === undefined) {
  68. done();
  69. } else {
  70. done.fail(new Error('Jest: `it` and `test` must return either a Promise or undefined.'));
  71. }
  72. };
  73. return originalFn.call(env, specName, asyncFn, timeout);
  74. };
  75. }
  76. function makeConcurrent(originalFn, env) {
  77. return function (specName, fn, timeout) {
  78. if (env != null && !env.specFilter({ getFullName: () => specName || '' })) {
  79. return originalFn.call(env, specName, () => Promise.resolve(), timeout);
  80. }
  81. let promise;
  82. try {
  83. promise = fn();
  84. if (!isPromise(promise)) {
  85. throw new Error(`Jest: concurrent test "${specName}" must return a Promise.`);
  86. }
  87. } catch (error) {
  88. return originalFn.call(env, specName, () => Promise.reject(error));
  89. }
  90. return originalFn.call(env, specName, () => promise, timeout);
  91. };
  92. }
  93. function install(global) {
  94. const jasmine = global.jasmine;
  95. const env = jasmine.getEnv();
  96. env.it = promisifyIt(env.it, env);
  97. env.fit = promisifyIt(env.fit, env);
  98. global.it.concurrent = makeConcurrent(env.it, env);
  99. global.it.concurrent.only = makeConcurrent(env.fit, env);
  100. global.it.concurrent.skip = makeConcurrent(env.xit, env);
  101. global.fit.concurrent = makeConcurrent(env.fit);
  102. env.afterAll = promisifyLifeCycleFunction(env.afterAll, env);
  103. env.afterEach = promisifyLifeCycleFunction(env.afterEach, env);
  104. env.beforeAll = promisifyLifeCycleFunction(env.beforeAll, env);
  105. env.beforeEach = promisifyLifeCycleFunction(env.beforeEach, env);
  106. }