index.js 7.6 KB

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