generateEmptyCoverage.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.default = generateEmptyCoverage;
  6. function fs() {
  7. const data = _interopRequireWildcard(require('graceful-fs'));
  8. fs = function () {
  9. return data;
  10. };
  11. return data;
  12. }
  13. function _istanbulLibCoverage() {
  14. const data = require('istanbul-lib-coverage');
  15. _istanbulLibCoverage = function () {
  16. return data;
  17. };
  18. return data;
  19. }
  20. function _istanbulLibInstrument() {
  21. const data = require('istanbul-lib-instrument');
  22. _istanbulLibInstrument = function () {
  23. return data;
  24. };
  25. return data;
  26. }
  27. function _transform() {
  28. const data = require('@jest/transform');
  29. _transform = function () {
  30. return data;
  31. };
  32. return data;
  33. }
  34. function _getRequireWildcardCache(nodeInterop) {
  35. if (typeof WeakMap !== 'function') return null;
  36. var cacheBabelInterop = new WeakMap();
  37. var cacheNodeInterop = new WeakMap();
  38. return (_getRequireWildcardCache = function (nodeInterop) {
  39. return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
  40. })(nodeInterop);
  41. }
  42. function _interopRequireWildcard(obj, nodeInterop) {
  43. if (!nodeInterop && obj && obj.__esModule) {
  44. return obj;
  45. }
  46. if (obj === null || (typeof obj !== 'object' && typeof obj !== 'function')) {
  47. return {default: obj};
  48. }
  49. var cache = _getRequireWildcardCache(nodeInterop);
  50. if (cache && cache.has(obj)) {
  51. return cache.get(obj);
  52. }
  53. var newObj = {};
  54. var hasPropertyDescriptor =
  55. Object.defineProperty && Object.getOwnPropertyDescriptor;
  56. for (var key in obj) {
  57. if (key !== 'default' && Object.prototype.hasOwnProperty.call(obj, key)) {
  58. var desc = hasPropertyDescriptor
  59. ? Object.getOwnPropertyDescriptor(obj, key)
  60. : null;
  61. if (desc && (desc.get || desc.set)) {
  62. Object.defineProperty(newObj, key, desc);
  63. } else {
  64. newObj[key] = obj[key];
  65. }
  66. }
  67. }
  68. newObj.default = obj;
  69. if (cache) {
  70. cache.set(obj, newObj);
  71. }
  72. return newObj;
  73. }
  74. /**
  75. * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  76. *
  77. * This source code is licensed under the MIT license found in the
  78. * LICENSE file in the root directory of this source tree.
  79. */
  80. async function generateEmptyCoverage(
  81. source,
  82. filename,
  83. globalConfig,
  84. config,
  85. changedFiles,
  86. sourcesRelatedToTestsInChangedFiles
  87. ) {
  88. const coverageOptions = {
  89. changedFiles,
  90. collectCoverage: globalConfig.collectCoverage,
  91. collectCoverageFrom: globalConfig.collectCoverageFrom,
  92. collectCoverageOnlyFrom: globalConfig.collectCoverageOnlyFrom,
  93. coverageProvider: globalConfig.coverageProvider,
  94. sourcesRelatedToTestsInChangedFiles
  95. };
  96. let coverageWorkerResult = null;
  97. if ((0, _transform().shouldInstrument)(filename, coverageOptions, config)) {
  98. if (coverageOptions.coverageProvider === 'v8') {
  99. const stat = fs().statSync(filename);
  100. return {
  101. kind: 'V8Coverage',
  102. result: {
  103. functions: [
  104. {
  105. functionName: '(empty-report)',
  106. isBlockCoverage: true,
  107. ranges: [
  108. {
  109. count: 0,
  110. endOffset: stat.size,
  111. startOffset: 0
  112. }
  113. ]
  114. }
  115. ],
  116. scriptId: '0',
  117. url: filename
  118. }
  119. };
  120. }
  121. const scriptTransformer = await (0, _transform().createScriptTransformer)(
  122. config
  123. ); // Transform file with instrumentation to make sure initial coverage data is well mapped to original code.
  124. const {code} = await scriptTransformer.transformSourceAsync(
  125. filename,
  126. source,
  127. {
  128. instrument: true,
  129. supportsDynamicImport: true,
  130. supportsExportNamespaceFrom: true,
  131. supportsStaticESM: true,
  132. supportsTopLevelAwait: true
  133. }
  134. ); // TODO: consider passing AST
  135. const extracted = (0, _istanbulLibInstrument().readInitialCoverage)(code); // Check extracted initial coverage is not null, this can happen when using /* istanbul ignore file */
  136. if (extracted) {
  137. coverageWorkerResult = {
  138. coverage: (0, _istanbulLibCoverage().createFileCoverage)(
  139. extracted.coverageData
  140. ),
  141. kind: 'BabelCoverage'
  142. };
  143. }
  144. }
  145. return coverageWorkerResult;
  146. }