shouldInstrument.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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() {
  38. if (typeof WeakMap !== 'function') return null;
  39. var cache = new WeakMap();
  40. _getRequireWildcardCache = function () {
  41. return cache;
  42. };
  43. return cache;
  44. }
  45. function _interopRequireWildcard(obj) {
  46. if (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();
  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 (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) Facebook, Inc. and its affiliates. All Rights Reserved.
  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); // prevent stateful regexes from breaking, just in case
  94. regex.lastIndex = 0;
  95. return regex;
  96. };
  97. function shouldInstrument(filename, options, config) {
  98. if (!options.collectCoverage) {
  99. return false;
  100. }
  101. if (
  102. config.forceCoverageMatch.length &&
  103. _micromatch().default.any(filename, config.forceCoverageMatch)
  104. ) {
  105. return true;
  106. }
  107. if (
  108. !config.testPathIgnorePatterns.some(pattern =>
  109. getRegex(pattern).test(filename)
  110. )
  111. ) {
  112. if (config.testRegex.some(regex => new RegExp(regex).test(filename))) {
  113. return false;
  114. }
  115. if (
  116. (0, _jestUtil().globsToMatcher)(config.testMatch)(
  117. (0, _jestUtil().replacePathSepForGlob)(filename)
  118. )
  119. ) {
  120. return false;
  121. }
  122. }
  123. if (
  124. // This configuration field contains an object in the form of:
  125. // {'path/to/file.js': true}
  126. options.collectCoverageOnlyFrom &&
  127. !options.collectCoverageOnlyFrom[filename]
  128. ) {
  129. return false;
  130. }
  131. if (
  132. // still cover if `only` is specified
  133. !options.collectCoverageOnlyFrom &&
  134. options.collectCoverageFrom.length &&
  135. !(0, _jestUtil().globsToMatcher)(options.collectCoverageFrom)(
  136. (0, _jestUtil().replacePathSepForGlob)(
  137. path().relative(config.rootDir, filename)
  138. )
  139. )
  140. ) {
  141. return false;
  142. }
  143. if (
  144. config.coveragePathIgnorePatterns.some(pattern => !!filename.match(pattern))
  145. ) {
  146. return false;
  147. }
  148. if (config.globalSetup === filename) {
  149. return false;
  150. }
  151. if (config.globalTeardown === filename) {
  152. return false;
  153. }
  154. if (config.setupFiles.includes(filename)) {
  155. return false;
  156. }
  157. if (config.setupFilesAfterEnv.includes(filename)) {
  158. return false;
  159. }
  160. if (MOCKS_PATTERN.test(filename)) {
  161. return false;
  162. }
  163. if (options.changedFiles && !options.changedFiles.has(filename)) {
  164. if (!options.sourcesRelatedToTestsInChangedFiles) {
  165. return false;
  166. }
  167. if (!options.sourcesRelatedToTestsInChangedFiles.has(filename)) {
  168. return false;
  169. }
  170. }
  171. return true;
  172. }