shouldInstrument.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.default = shouldInstrument;
  6. function path() {
  7. const data = _interopRequireWildcard(require('path'));
  8. path = function () {
  9. return data;
  10. };
  11. return data;
  12. }
  13. function _micromatch() {
  14. const data = _interopRequireDefault(require('micromatch'));
  15. _micromatch = function () {
  16. return data;
  17. };
  18. return data;
  19. }
  20. function _jestRegexUtil() {
  21. const data = require('jest-regex-util');
  22. _jestRegexUtil = function () {
  23. return data;
  24. };
  25. return data;
  26. }
  27. function _jestUtil() {
  28. const data = require('jest-util');
  29. _jestUtil = function () {
  30. return data;
  31. };
  32. return data;
  33. }
  34. function _interopRequireDefault(obj) {
  35. return obj && obj.__esModule ? obj : {default: obj};
  36. }
  37. function _getRequireWildcardCache(nodeInterop) {
  38. if (typeof WeakMap !== 'function') return null;
  39. var cacheBabelInterop = new WeakMap();
  40. var cacheNodeInterop = new WeakMap();
  41. return (_getRequireWildcardCache = function (nodeInterop) {
  42. return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
  43. })(nodeInterop);
  44. }
  45. function _interopRequireWildcard(obj, nodeInterop) {
  46. if (!nodeInterop && obj && obj.__esModule) {
  47. return obj;
  48. }
  49. if (obj === null || (typeof obj !== 'object' && typeof obj !== 'function')) {
  50. return {default: obj};
  51. }
  52. var cache = _getRequireWildcardCache(nodeInterop);
  53. if (cache && cache.has(obj)) {
  54. return cache.get(obj);
  55. }
  56. var newObj = {};
  57. var hasPropertyDescriptor =
  58. Object.defineProperty && Object.getOwnPropertyDescriptor;
  59. for (var key in obj) {
  60. if (key !== 'default' && Object.prototype.hasOwnProperty.call(obj, key)) {
  61. var desc = hasPropertyDescriptor
  62. ? Object.getOwnPropertyDescriptor(obj, key)
  63. : null;
  64. if (desc && (desc.get || desc.set)) {
  65. Object.defineProperty(newObj, key, desc);
  66. } else {
  67. newObj[key] = obj[key];
  68. }
  69. }
  70. }
  71. newObj.default = obj;
  72. if (cache) {
  73. cache.set(obj, newObj);
  74. }
  75. return newObj;
  76. }
  77. /**
  78. * Copyright (c) Meta Platforms, Inc. and affiliates.
  79. *
  80. * This source code is licensed under the MIT license found in the
  81. * LICENSE file in the root directory of this source tree.
  82. */
  83. const MOCKS_PATTERN = new RegExp(
  84. (0, _jestRegexUtil().escapePathForRegex)(
  85. `${path().sep}__mocks__${path().sep}`
  86. )
  87. );
  88. const cachedRegexes = new Map();
  89. const getRegex = regexStr => {
  90. if (!cachedRegexes.has(regexStr)) {
  91. cachedRegexes.set(regexStr, new RegExp(regexStr));
  92. }
  93. const regex = cachedRegexes.get(regexStr);
  94. // prevent stateful regexes from breaking, just in case
  95. regex.lastIndex = 0;
  96. return regex;
  97. };
  98. function shouldInstrument(filename, options, config, loadedFilenames) {
  99. if (!options.collectCoverage) {
  100. return false;
  101. }
  102. if (
  103. config.forceCoverageMatch.length &&
  104. _micromatch().default.any(filename, config.forceCoverageMatch)
  105. ) {
  106. return true;
  107. }
  108. if (
  109. !config.testPathIgnorePatterns.some(pattern =>
  110. getRegex(pattern).test(filename)
  111. )
  112. ) {
  113. if (config.testRegex.some(regex => new RegExp(regex).test(filename))) {
  114. return false;
  115. }
  116. if (
  117. (0, _jestUtil().globsToMatcher)(config.testMatch)(
  118. (0, _jestUtil().replacePathSepForGlob)(filename)
  119. )
  120. ) {
  121. return false;
  122. }
  123. }
  124. if (
  125. options.collectCoverageFrom.length === 0 &&
  126. loadedFilenames != null &&
  127. !loadedFilenames.includes(filename)
  128. ) {
  129. return false;
  130. }
  131. if (
  132. // still cover if `only` is specified
  133. options.collectCoverageFrom.length &&
  134. !(0, _jestUtil().globsToMatcher)(options.collectCoverageFrom)(
  135. (0, _jestUtil().replacePathSepForGlob)(
  136. path().relative(config.rootDir, filename)
  137. )
  138. )
  139. ) {
  140. return false;
  141. }
  142. if (
  143. config.coveragePathIgnorePatterns.some(pattern => !!filename.match(pattern))
  144. ) {
  145. return false;
  146. }
  147. if (config.globalSetup === filename) {
  148. return false;
  149. }
  150. if (config.globalTeardown === filename) {
  151. return false;
  152. }
  153. if (config.setupFiles.includes(filename)) {
  154. return false;
  155. }
  156. if (config.setupFilesAfterEnv.includes(filename)) {
  157. return false;
  158. }
  159. if (MOCKS_PATTERN.test(filename)) {
  160. return false;
  161. }
  162. if (options.changedFiles && !options.changedFiles.has(filename)) {
  163. if (!options.sourcesRelatedToTestsInChangedFiles) {
  164. return false;
  165. }
  166. if (!options.sourcesRelatedToTestsInChangedFiles.has(filename)) {
  167. return false;
  168. }
  169. }
  170. if (filename.endsWith('.json')) {
  171. return false;
  172. }
  173. return true;
  174. }