jestAdapter.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. 'use strict';
  2. var _jestUtil = require('jest-util');
  3. /**
  4. * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  5. *
  6. * This source code is licensed under the MIT license found in the
  7. * LICENSE file in the root directory of this source tree.
  8. */
  9. const FRAMEWORK_INITIALIZER = require.resolve('./jestAdapterInit');
  10. const jestAdapter = async (
  11. globalConfig,
  12. config,
  13. environment,
  14. runtime,
  15. testPath,
  16. sendMessageToJest
  17. ) => {
  18. var _runtime$setGlobalsFo, _runtime$unstable_sho2;
  19. const {
  20. initialize,
  21. runAndTransformResultsToJestFormat
  22. } = runtime.requireInternalModule(FRAMEWORK_INITIALIZER);
  23. const getPrettier = () =>
  24. config.prettierPath ? require(config.prettierPath) : null;
  25. const getBabelTraverse = () => require('@babel/traverse').default;
  26. const {globals, snapshotState} = await initialize({
  27. config,
  28. environment,
  29. getBabelTraverse,
  30. getPrettier,
  31. globalConfig,
  32. localRequire: runtime.requireModule.bind(runtime),
  33. parentProcess: process,
  34. sendMessageToJest,
  35. setGlobalsForRuntime:
  36. (_runtime$setGlobalsFo = runtime.setGlobalsForRuntime) === null ||
  37. _runtime$setGlobalsFo === void 0
  38. ? void 0
  39. : _runtime$setGlobalsFo.bind(runtime),
  40. testPath
  41. });
  42. if (config.timers === 'fake' || config.timers === 'legacy') {
  43. // during setup, this cannot be null (and it's fine to explode if it is)
  44. environment.fakeTimers.useFakeTimers();
  45. } else if (config.timers === 'modern') {
  46. environment.fakeTimersModern.useFakeTimers();
  47. }
  48. globals.beforeEach(() => {
  49. if (config.resetModules) {
  50. runtime.resetModules();
  51. }
  52. if (config.clearMocks) {
  53. runtime.clearAllMocks();
  54. }
  55. if (config.resetMocks) {
  56. runtime.resetAllMocks();
  57. if (config.timers === 'fake') {
  58. // during setup, this cannot be null (and it's fine to explode if it is)
  59. environment.fakeTimers.useFakeTimers();
  60. }
  61. }
  62. if (config.restoreMocks) {
  63. runtime.restoreAllMocks();
  64. }
  65. });
  66. for (const path of config.setupFilesAfterEnv) {
  67. var _runtime$unstable_sho;
  68. // TODO: remove ? in Jest 26
  69. const esm =
  70. (_runtime$unstable_sho = runtime.unstable_shouldLoadAsEsm) === null ||
  71. _runtime$unstable_sho === void 0
  72. ? void 0
  73. : _runtime$unstable_sho.call(runtime, path);
  74. if (esm) {
  75. await runtime.unstable_importModule(path);
  76. } else {
  77. runtime.requireModule(path);
  78. }
  79. } // TODO: remove ? in Jest 26
  80. const esm =
  81. (_runtime$unstable_sho2 = runtime.unstable_shouldLoadAsEsm) === null ||
  82. _runtime$unstable_sho2 === void 0
  83. ? void 0
  84. : _runtime$unstable_sho2.call(runtime, testPath);
  85. if (esm) {
  86. await runtime.unstable_importModule(testPath);
  87. } else {
  88. runtime.requireModule(testPath);
  89. }
  90. const results = await runAndTransformResultsToJestFormat({
  91. config,
  92. globalConfig,
  93. testPath
  94. });
  95. _addSnapshotData(results, snapshotState); // We need to copy the results object to ensure we don't leaks the prototypes
  96. // from the VM. Jasmine creates the result objects in the parent process, we
  97. // should consider doing that for circus as well.
  98. return (0, _jestUtil.deepCyclicCopy)(results, {
  99. keepPrototype: false
  100. });
  101. };
  102. const _addSnapshotData = (results, snapshotState) => {
  103. results.testResults.forEach(({fullName, status}) => {
  104. if (status === 'pending' || status === 'failed') {
  105. // if test is skipped or failed, we don't want to mark
  106. // its snapshots as obsolete.
  107. snapshotState.markSnapshotsAsCheckedForTest(fullName);
  108. }
  109. });
  110. const uncheckedCount = snapshotState.getUncheckedCount();
  111. const uncheckedKeys = snapshotState.getUncheckedKeys();
  112. if (uncheckedCount) {
  113. snapshotState.removeUncheckedKeys();
  114. }
  115. const status = snapshotState.save();
  116. results.snapshot.fileDeleted = status.deleted;
  117. results.snapshot.added = snapshotState.added;
  118. results.snapshot.matched = snapshotState.matched;
  119. results.snapshot.unmatched = snapshotState.unmatched;
  120. results.snapshot.updated = snapshotState.updated;
  121. results.snapshot.unchecked = !status.deleted ? uncheckedCount : 0; // Copy the array to prevent memory leaks
  122. results.snapshot.uncheckedKeys = Array.from(uncheckedKeys);
  123. };
  124. module.exports = jestAdapter;