shouldInstrument.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.default = shouldInstrument;
  6. function _path() {
  7. const data = _interopRequireDefault(require('path'));
  8. _path = function _path() {
  9. return data;
  10. };
  11. return data;
  12. }
  13. function _jestRegexUtil() {
  14. const data = require('jest-regex-util');
  15. _jestRegexUtil = function _jestRegexUtil() {
  16. return data;
  17. };
  18. return data;
  19. }
  20. function _jestUtil() {
  21. const data = require('jest-util');
  22. _jestUtil = function _jestUtil() {
  23. return data;
  24. };
  25. return data;
  26. }
  27. function _micromatch() {
  28. const data = _interopRequireDefault(require('micromatch'));
  29. _micromatch = function _micromatch() {
  30. return data;
  31. };
  32. return data;
  33. }
  34. function _interopRequireDefault(obj) {
  35. return obj && obj.__esModule ? obj : {default: obj};
  36. }
  37. /**
  38. * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  39. *
  40. * This source code is licensed under the MIT license found in the
  41. * LICENSE file in the root directory of this source tree.
  42. */
  43. const MOCKS_PATTERN = new RegExp(
  44. (0, _jestRegexUtil().escapePathForRegex)(
  45. _path().default.sep + '__mocks__' + _path().default.sep
  46. )
  47. );
  48. function shouldInstrument(filename, options, config) {
  49. if (!options.collectCoverage) {
  50. return false;
  51. }
  52. if (
  53. config.forceCoverageMatch.length &&
  54. _micromatch().default.any(filename, config.forceCoverageMatch)
  55. ) {
  56. return true;
  57. }
  58. if (
  59. !config.testPathIgnorePatterns.some(pattern => !!filename.match(pattern))
  60. ) {
  61. if (config.testRegex.some(regex => new RegExp(regex).test(filename))) {
  62. return false;
  63. }
  64. if (
  65. _micromatch().default.some(
  66. (0, _jestUtil().replacePathSepForGlob)(filename),
  67. config.testMatch
  68. )
  69. ) {
  70. return false;
  71. }
  72. }
  73. if (
  74. // This configuration field contains an object in the form of:
  75. // {'path/to/file.js': true}
  76. options.collectCoverageOnlyFrom &&
  77. !options.collectCoverageOnlyFrom[filename]
  78. ) {
  79. return false;
  80. }
  81. if (
  82. // still cover if `only` is specified
  83. !options.collectCoverageOnlyFrom &&
  84. options.collectCoverageFrom &&
  85. !_micromatch().default.some(
  86. (0, _jestUtil().replacePathSepForGlob)(
  87. _path().default.relative(config.rootDir, filename)
  88. ),
  89. options.collectCoverageFrom
  90. )
  91. ) {
  92. return false;
  93. }
  94. if (
  95. config.coveragePathIgnorePatterns.some(pattern => !!filename.match(pattern))
  96. ) {
  97. return false;
  98. }
  99. if (config.globalSetup === filename) {
  100. return false;
  101. }
  102. if (config.globalTeardown === filename) {
  103. return false;
  104. }
  105. if (config.setupFiles.includes(filename)) {
  106. return false;
  107. }
  108. if (config.setupFilesAfterEnv.includes(filename)) {
  109. return false;
  110. }
  111. if (MOCKS_PATTERN.test(filename)) {
  112. return false;
  113. }
  114. if (options.changedFiles && !options.changedFiles.has(filename)) {
  115. return false;
  116. }
  117. return true;
  118. }