index.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.default = jasmine2;
  6. var path = _interopRequireWildcard(require('path'));
  7. var _sourceMap = require('@jest/source-map');
  8. var _each = _interopRequireDefault(require('./each'));
  9. var _errorOnPrivate = require('./errorOnPrivate');
  10. var _jasmineAsyncInstall = _interopRequireDefault(
  11. require('./jasmineAsyncInstall')
  12. );
  13. var _reporter = _interopRequireDefault(require('./reporter'));
  14. function _interopRequireDefault(obj) {
  15. return obj && obj.__esModule ? obj : {default: obj};
  16. }
  17. function _getRequireWildcardCache(nodeInterop) {
  18. if (typeof WeakMap !== 'function') return null;
  19. var cacheBabelInterop = new WeakMap();
  20. var cacheNodeInterop = new WeakMap();
  21. return (_getRequireWildcardCache = function (nodeInterop) {
  22. return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
  23. })(nodeInterop);
  24. }
  25. function _interopRequireWildcard(obj, nodeInterop) {
  26. if (!nodeInterop && obj && obj.__esModule) {
  27. return obj;
  28. }
  29. if (obj === null || (typeof obj !== 'object' && typeof obj !== 'function')) {
  30. return {default: obj};
  31. }
  32. var cache = _getRequireWildcardCache(nodeInterop);
  33. if (cache && cache.has(obj)) {
  34. return cache.get(obj);
  35. }
  36. var newObj = {};
  37. var hasPropertyDescriptor =
  38. Object.defineProperty && Object.getOwnPropertyDescriptor;
  39. for (var key in obj) {
  40. if (key !== 'default' && Object.prototype.hasOwnProperty.call(obj, key)) {
  41. var desc = hasPropertyDescriptor
  42. ? Object.getOwnPropertyDescriptor(obj, key)
  43. : null;
  44. if (desc && (desc.get || desc.set)) {
  45. Object.defineProperty(newObj, key, desc);
  46. } else {
  47. newObj[key] = obj[key];
  48. }
  49. }
  50. }
  51. newObj.default = obj;
  52. if (cache) {
  53. cache.set(obj, newObj);
  54. }
  55. return newObj;
  56. }
  57. /**
  58. * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  59. *
  60. * This source code is licensed under the MIT license found in the
  61. * LICENSE file in the root directory of this source tree.
  62. */
  63. const JASMINE = require.resolve('./jasmine/jasmineLight');
  64. const jestEachBuildDir = path.dirname(require.resolve('jest-each'));
  65. async function jasmine2(globalConfig, config, environment, runtime, testPath) {
  66. const reporter = new _reporter.default(globalConfig, config, testPath);
  67. const jasmineFactory = runtime.requireInternalModule(JASMINE);
  68. const jasmine = jasmineFactory.create({
  69. process,
  70. testPath,
  71. testTimeout: globalConfig.testTimeout
  72. });
  73. const env = jasmine.getEnv();
  74. const jasmineInterface = jasmineFactory._interface(jasmine, env);
  75. Object.assign(environment.global, jasmineInterface);
  76. env.addReporter(jasmineInterface.jsApiReporter); // TODO: Remove config option if V8 exposes some way of getting location of caller
  77. // in a future version
  78. if (config.testLocationInResults === true) {
  79. function wrapIt(original) {
  80. const wrapped = (testName, fn, timeout) => {
  81. var _stack$getFileName;
  82. const sourcemaps = runtime.getSourceMaps();
  83. let stack = (0, _sourceMap.getCallsite)(1, sourcemaps);
  84. const it = original(testName, fn, timeout);
  85. if (
  86. (_stack$getFileName = stack.getFileName()) !== null &&
  87. _stack$getFileName !== void 0 &&
  88. _stack$getFileName.startsWith(jestEachBuildDir)
  89. ) {
  90. stack = (0, _sourceMap.getCallsite)(4, sourcemaps);
  91. } // @ts-expect-error
  92. it.result.__callsite = stack;
  93. return it;
  94. };
  95. return wrapped;
  96. }
  97. environment.global.it = wrapIt(environment.global.it);
  98. environment.global.xit = wrapIt(environment.global.xit);
  99. environment.global.fit = wrapIt(environment.global.fit);
  100. }
  101. (0, _jasmineAsyncInstall.default)(globalConfig, environment.global);
  102. (0, _each.default)(environment);
  103. environment.global.test = environment.global.it;
  104. environment.global.it.only = environment.global.fit;
  105. environment.global.it.todo = env.todo;
  106. environment.global.it.skip = environment.global.xit;
  107. environment.global.xtest = environment.global.xit;
  108. environment.global.describe.skip = environment.global.xdescribe;
  109. environment.global.describe.only = environment.global.fdescribe;
  110. if (config.timers === 'fake' || config.timers === 'modern') {
  111. environment.fakeTimersModern.useFakeTimers();
  112. } else if (config.timers === 'legacy') {
  113. environment.fakeTimers.useFakeTimers();
  114. }
  115. env.beforeEach(() => {
  116. if (config.resetModules) {
  117. runtime.resetModules();
  118. }
  119. if (config.clearMocks) {
  120. runtime.clearAllMocks();
  121. }
  122. if (config.resetMocks) {
  123. runtime.resetAllMocks();
  124. if (config.timers === 'legacy') {
  125. environment.fakeTimers.useFakeTimers();
  126. }
  127. }
  128. if (config.restoreMocks) {
  129. runtime.restoreAllMocks();
  130. }
  131. });
  132. env.addReporter(reporter);
  133. runtime
  134. .requireInternalModule(path.resolve(__dirname, './jestExpect.js'))
  135. .default({
  136. expand: globalConfig.expand
  137. });
  138. if (globalConfig.errorOnDeprecated) {
  139. (0, _errorOnPrivate.installErrorOnPrivate)(environment.global);
  140. } else {
  141. Object.defineProperty(jasmine, 'DEFAULT_TIMEOUT_INTERVAL', {
  142. configurable: true,
  143. enumerable: true,
  144. get() {
  145. return this._DEFAULT_TIMEOUT_INTERVAL;
  146. },
  147. set(value) {
  148. this._DEFAULT_TIMEOUT_INTERVAL = value;
  149. }
  150. });
  151. }
  152. const snapshotState = await runtime
  153. .requireInternalModule(path.resolve(__dirname, './setup_jest_globals.js'))
  154. .default({
  155. config,
  156. globalConfig,
  157. localRequire: runtime.requireModule.bind(runtime),
  158. testPath
  159. });
  160. for (const path of config.setupFilesAfterEnv) {
  161. const esm = runtime.unstable_shouldLoadAsEsm(path);
  162. if (esm) {
  163. await runtime.unstable_importModule(path);
  164. } else {
  165. runtime.requireModule(path);
  166. }
  167. }
  168. if (globalConfig.testNamePattern) {
  169. const testNameRegex = new RegExp(globalConfig.testNamePattern, 'i');
  170. env.specFilter = spec => testNameRegex.test(spec.getFullName());
  171. }
  172. const esm = runtime.unstable_shouldLoadAsEsm(testPath);
  173. if (esm) {
  174. await runtime.unstable_importModule(testPath);
  175. } else {
  176. runtime.requireModule(testPath);
  177. }
  178. await env.execute();
  179. const results = await reporter.getResults();
  180. return addSnapshotData(results, snapshotState);
  181. }
  182. const addSnapshotData = (results, snapshotState) => {
  183. results.testResults.forEach(({fullName, status}) => {
  184. if (status === 'pending' || status === 'failed') {
  185. // if test is skipped or failed, we don't want to mark
  186. // its snapshots as obsolete.
  187. snapshotState.markSnapshotsAsCheckedForTest(fullName);
  188. }
  189. });
  190. const uncheckedCount = snapshotState.getUncheckedCount();
  191. const uncheckedKeys = snapshotState.getUncheckedKeys();
  192. if (uncheckedCount) {
  193. snapshotState.removeUncheckedKeys();
  194. }
  195. const status = snapshotState.save();
  196. results.snapshot.fileDeleted = status.deleted;
  197. results.snapshot.added = snapshotState.added;
  198. results.snapshot.matched = snapshotState.matched;
  199. results.snapshot.unmatched = snapshotState.unmatched;
  200. results.snapshot.updated = snapshotState.updated;
  201. results.snapshot.unchecked = !status.deleted ? uncheckedCount : 0; // Copy the array to prevent memory leaks
  202. results.snapshot.uncheckedKeys = Array.from(uncheckedKeys);
  203. return results;
  204. };