index.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. 'use strict';
  2. let jasmine2 = (() => {
  3. var _ref = _asyncToGenerator(function*(
  4. globalConfig,
  5. config,
  6. environment,
  7. runtime,
  8. testPath
  9. ) {
  10. const reporter = new _reporter2.default(globalConfig, config, testPath);
  11. const jasmineFactory = runtime.requireInternalModule(JASMINE);
  12. const jasmine = jasmineFactory.create({
  13. process: process,
  14. testPath: testPath
  15. });
  16. const env = jasmine.getEnv();
  17. const jasmineInterface = jasmineFactory.interface(jasmine, env);
  18. Object.assign(environment.global, jasmineInterface);
  19. env.addReporter(jasmineInterface.jsApiReporter);
  20. // TODO: Remove config option if V8 exposes some way of getting location of caller
  21. // in a future version
  22. if (config.testLocationInResults === true) {
  23. const originalIt = environment.global.it;
  24. environment.global.it = function() {
  25. const stack = (0, _jestUtil.getCallsite)(1, runtime.getSourceMaps());
  26. const it = originalIt.apply(undefined, arguments);
  27. it.result.__callsite = stack;
  28. return it;
  29. };
  30. }
  31. (0, _jasmine_async.install)(environment.global);
  32. (0, _each2.default)(environment);
  33. environment.global.test = environment.global.it;
  34. environment.global.it.only = environment.global.fit;
  35. environment.global.it.skip = environment.global.xit;
  36. environment.global.xtest = environment.global.xit;
  37. environment.global.describe.skip = environment.global.xdescribe;
  38. environment.global.describe.only = environment.global.fdescribe;
  39. if (config.timers === 'fake') {
  40. environment.fakeTimers.useFakeTimers();
  41. }
  42. env.beforeEach(function() {
  43. if (config.resetModules) {
  44. runtime.resetModules();
  45. }
  46. if (config.clearMocks) {
  47. runtime.clearAllMocks();
  48. }
  49. if (config.resetMocks) {
  50. runtime.resetAllMocks();
  51. if (config.timers === 'fake') {
  52. environment.fakeTimers.useFakeTimers();
  53. }
  54. }
  55. if (config.restoreMocks) {
  56. runtime.restoreAllMocks();
  57. }
  58. });
  59. env.addReporter(reporter);
  60. runtime
  61. .requireInternalModule(
  62. _path2.default.resolve(__dirname, './jest_expect.js')
  63. )
  64. .default({
  65. expand: globalConfig.expand
  66. });
  67. if (globalConfig.errorOnDeprecated) {
  68. (0, _error_on_private.installErrorOnPrivate)(environment.global);
  69. } else {
  70. // $FlowFixMe Flow seems to be confused about accessors and tries to enfoce having a `value` property.
  71. Object.defineProperty(jasmine, 'DEFAULT_TIMEOUT_INTERVAL', {
  72. configurable: true,
  73. enumerable: true,
  74. get: function() {
  75. return this._DEFAULT_TIMEOUT_INTERVAL;
  76. },
  77. set: function(value) {
  78. this._DEFAULT_TIMEOUT_INTERVAL = value;
  79. }
  80. });
  81. }
  82. const snapshotState = runtime
  83. .requireInternalModule(
  84. _path2.default.resolve(__dirname, './setup_jest_globals.js')
  85. )
  86. .default({
  87. config: config,
  88. globalConfig: globalConfig,
  89. localRequire: runtime.requireModule.bind(runtime),
  90. testPath: testPath
  91. });
  92. if (config.setupTestFrameworkScriptFile) {
  93. runtime.requireModule(config.setupTestFrameworkScriptFile);
  94. }
  95. if (globalConfig.enabledTestsMap) {
  96. env.specFilter = function(spec) {
  97. const suiteMap =
  98. globalConfig.enabledTestsMap &&
  99. globalConfig.enabledTestsMap[spec.result.testPath];
  100. return suiteMap && suiteMap[spec.result.fullName];
  101. };
  102. } else if (globalConfig.testNamePattern) {
  103. const testNameRegex = new RegExp(globalConfig.testNamePattern, 'i');
  104. env.specFilter = function(spec) {
  105. return testNameRegex.test(spec.getFullName());
  106. };
  107. }
  108. runtime.requireModule(testPath);
  109. yield env.execute();
  110. const results = yield reporter.getResults();
  111. return addSnapshotData(results, snapshotState);
  112. });
  113. return function jasmine2(_x, _x2, _x3, _x4, _x5) {
  114. return _ref.apply(this, arguments);
  115. };
  116. })();
  117. var _path = require('path');
  118. var _path2 = _interopRequireDefault(_path);
  119. var _each = require('./each');
  120. var _each2 = _interopRequireDefault(_each);
  121. var _error_on_private = require('./error_on_private');
  122. var _jestUtil = require('jest-util');
  123. var _reporter = require('./reporter');
  124. var _reporter2 = _interopRequireDefault(_reporter);
  125. var _jasmine_async = require('./jasmine_async');
  126. function _interopRequireDefault(obj) {
  127. return obj && obj.__esModule ? obj : {default: obj};
  128. }
  129. function _asyncToGenerator(fn) {
  130. return function() {
  131. var gen = fn.apply(this, arguments);
  132. return new Promise(function(resolve, reject) {
  133. function step(key, arg) {
  134. try {
  135. var info = gen[key](arg);
  136. var value = info.value;
  137. } catch (error) {
  138. reject(error);
  139. return;
  140. }
  141. if (info.done) {
  142. resolve(value);
  143. } else {
  144. return Promise.resolve(value).then(
  145. function(value) {
  146. step('next', value);
  147. },
  148. function(err) {
  149. step('throw', err);
  150. }
  151. );
  152. }
  153. }
  154. return step('next');
  155. });
  156. };
  157. }
  158. // eslint-disable-next-line import/no-extraneous-dependencies
  159. /**
  160. * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
  161. *
  162. * This source code is licensed under the MIT license found in the
  163. * LICENSE file in the root directory of this source tree.
  164. *
  165. *
  166. */
  167. const JASMINE = require.resolve('./jasmine/jasmine_light.js');
  168. const addSnapshotData = (results, snapshotState) => {
  169. results.testResults.forEach(_ref2 => {
  170. let fullName = _ref2.fullName,
  171. status = _ref2.status;
  172. if (status === 'pending' || status === 'failed') {
  173. // if test is skipped or failed, we don't want to mark
  174. // its snapshots as obsolete.
  175. snapshotState.markSnapshotsAsCheckedForTest(fullName);
  176. }
  177. });
  178. const uncheckedCount = snapshotState.getUncheckedCount();
  179. const uncheckedKeys = snapshotState.getUncheckedKeys();
  180. if (uncheckedCount) {
  181. snapshotState.removeUncheckedKeys();
  182. }
  183. const status = snapshotState.save();
  184. results.snapshot.fileDeleted = status.deleted;
  185. results.snapshot.added = snapshotState.added;
  186. results.snapshot.matched = snapshotState.matched;
  187. results.snapshot.unmatched = snapshotState.unmatched;
  188. results.snapshot.updated = snapshotState.updated;
  189. results.snapshot.unchecked = !status.deleted ? uncheckedCount : 0;
  190. // Copy the array to prevent memory leaks
  191. results.snapshot.uncheckedKeys = Array.from(uncheckedKeys);
  192. return results;
  193. };
  194. module.exports = jasmine2;