run_test.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. // Keeping the core of "runTest" as a separate function (as "runTestInternal")
  6. // is key to be able to detect memory leaks. Since all variables are local to
  7. // the function, when "runTestInternal" finishes its execution, they can all be
  8. // freed, UNLESS something else is leaking them (and that's why we can detect
  9. // the leak!).
  10. //
  11. // If we had all the code in a single function, we should manually nullify all
  12. // references to verify if there is a leak, which is not maintainable and error
  13. // prone. That's why "runTestInternal" CANNOT be inlined inside "runTest".
  14. let runTestInternal = (() => {
  15. var _ref = _asyncToGenerator(function* (path, globalConfig, config, resolver) {
  16. const testSource = (_fs || _load_fs()).default.readFileSync(path, 'utf8');
  17. const parsedDocblock = (_jestDocblock || _load_jestDocblock()).parse((_jestDocblock || _load_jestDocblock()).extract(testSource));
  18. const customEnvironment = parsedDocblock['jest-environment'];
  19. let testEnvironment = config.testEnvironment;
  20. if (customEnvironment) {
  21. testEnvironment = (0, (_jestConfig || _load_jestConfig()).getTestEnvironment)(Object.assign({}, config, {
  22. testEnvironment: customEnvironment
  23. }));
  24. }
  25. /* $FlowFixMe */
  26. const TestEnvironment = require(testEnvironment);
  27. /* $FlowFixMe */
  28. const testFramework = require(config.testRunner);
  29. /* $FlowFixMe */
  30. const Runtime = require(config.moduleLoader || 'jest-runtime');
  31. let runtime = undefined;
  32. const consoleOut = globalConfig.useStderr ? process.stderr : process.stdout;
  33. const consoleFormatter = function (type, message) {
  34. return (0, (_jestUtil || _load_jestUtil()).getConsoleOutput)(config.cwd, !!globalConfig.verbose,
  35. // 4 = the console call is buried 4 stack frames deep
  36. (_jestUtil || _load_jestUtil()).BufferedConsole.write([], type, message, 4, runtime && runtime.getSourceMaps()));
  37. };
  38. let testConsole;
  39. if (globalConfig.silent) {
  40. testConsole = new (_jestUtil || _load_jestUtil()).NullConsole(consoleOut, process.stderr, consoleFormatter);
  41. } else if (globalConfig.verbose) {
  42. testConsole = new (_jestUtil || _load_jestUtil()).Console(consoleOut, process.stderr, consoleFormatter);
  43. } else {
  44. testConsole = new (_jestUtil || _load_jestUtil()).BufferedConsole(function () {
  45. return runtime && runtime.getSourceMaps();
  46. });
  47. }
  48. const environment = new TestEnvironment(config, { console: testConsole });
  49. const leakDetector = config.detectLeaks ? new (_jestLeakDetector || _load_jestLeakDetector()).default(environment) : null;
  50. const cacheFS = { [path]: testSource };
  51. (0, (_jestUtil || _load_jestUtil()).setGlobal)(environment.global, 'console', testConsole);
  52. runtime = new Runtime(config, environment, resolver, cacheFS, {
  53. collectCoverage: globalConfig.collectCoverage,
  54. collectCoverageFrom: globalConfig.collectCoverageFrom,
  55. collectCoverageOnlyFrom: globalConfig.collectCoverageOnlyFrom
  56. });
  57. const start = Date.now();
  58. yield environment.setup();
  59. try {
  60. const result = yield testFramework(globalConfig, config, environment, runtime, path);
  61. const testCount = result.numPassingTests + result.numFailingTests + result.numPendingTests;
  62. result.perfStats = { end: Date.now(), start };
  63. result.testFilePath = path;
  64. result.coverage = runtime.getAllCoverageInfoCopy();
  65. result.sourceMaps = runtime.getSourceMapInfo(new Set(Object.keys(result.coverage || {})));
  66. result.console = testConsole.getBuffer();
  67. result.skipped = testCount === result.numPendingTests;
  68. result.displayName = config.displayName;
  69. if (globalConfig.logHeapUsage) {
  70. if (global.gc) {
  71. global.gc();
  72. }
  73. result.memoryUsage = process.memoryUsage().heapUsed;
  74. }
  75. // Delay the resolution to allow log messages to be output.
  76. return new Promise(function (resolve) {
  77. setImmediate(function () {
  78. return resolve({ leakDetector, result });
  79. });
  80. });
  81. } finally {
  82. yield environment.teardown();
  83. }
  84. });
  85. return function runTestInternal(_x, _x2, _x3, _x4) {
  86. return _ref.apply(this, arguments);
  87. };
  88. })();
  89. var _fs;
  90. function _load_fs() {
  91. return _fs = _interopRequireDefault(require('fs'));
  92. }
  93. var _jestUtil;
  94. function _load_jestUtil() {
  95. return _jestUtil = require('jest-util');
  96. }
  97. var _jestJasmine;
  98. function _load_jestJasmine() {
  99. return _jestJasmine = _interopRequireDefault(require('jest-jasmine2'));
  100. }
  101. var _jestLeakDetector;
  102. function _load_jestLeakDetector() {
  103. return _jestLeakDetector = _interopRequireDefault(require('jest-leak-detector'));
  104. }
  105. var _jestConfig;
  106. function _load_jestConfig() {
  107. return _jestConfig = require('jest-config');
  108. }
  109. var _jestDocblock;
  110. function _load_jestDocblock() {
  111. return _jestDocblock = _interopRequireWildcard(require('jest-docblock'));
  112. }
  113. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
  114. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  115. function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; } /**
  116. * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
  117. *
  118. * This source code is licensed under the MIT license found in the
  119. * LICENSE file in the root directory of this source tree.
  120. *
  121. *
  122. */
  123. // The default jest-runner is required because it is the default test runner
  124. // and required implicitly through the `testRunner` ProjectConfig option.
  125. (_jestJasmine || _load_jestJasmine()).default;
  126. exports.default = (() => {
  127. var _ref2 = _asyncToGenerator(function* (path, globalConfig, config, resolver) {
  128. var _ref3 = yield runTestInternal(path, globalConfig, config, resolver);
  129. const leakDetector = _ref3.leakDetector,
  130. result = _ref3.result;
  131. // Resolve leak detector, outside the "runTestInternal" closure.
  132. result.leaks = leakDetector ? leakDetector.isLeaking() : false;
  133. return result;
  134. });
  135. function runTest(_x5, _x6, _x7, _x8) {
  136. return _ref2.apply(this, arguments);
  137. }
  138. return runTest;
  139. })();