should_instrument.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.default = shouldInstrument;
  6. var _path;
  7. function _load_path() {
  8. return (_path = _interopRequireDefault(require('path')));
  9. }
  10. var _jestRegexUtil;
  11. function _load_jestRegexUtil() {
  12. return (_jestRegexUtil = require('jest-regex-util'));
  13. }
  14. var _micromatch;
  15. function _load_micromatch() {
  16. return (_micromatch = _interopRequireDefault(require('micromatch')));
  17. }
  18. function _interopRequireDefault(obj) {
  19. return obj && obj.__esModule ? obj : {default: obj};
  20. }
  21. const MOCKS_PATTERN = new RegExp(
  22. (0, (_jestRegexUtil || _load_jestRegexUtil()).escapePathForRegex)(
  23. (_path || _load_path()).default.sep +
  24. '__mocks__' +
  25. (_path || _load_path()).default.sep
  26. )
  27. );
  28. /**
  29. * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
  30. *
  31. * This source code is licensed under the MIT license found in the
  32. * LICENSE file in the root directory of this source tree.
  33. *
  34. *
  35. */
  36. function shouldInstrument(filename, options, config) {
  37. if (!options.collectCoverage) {
  38. return false;
  39. }
  40. if (
  41. config.forceCoverageMatch &&
  42. config.forceCoverageMatch.length &&
  43. (_micromatch || _load_micromatch()).default.any(
  44. filename,
  45. config.forceCoverageMatch
  46. )
  47. ) {
  48. return true;
  49. }
  50. if (config.testRegex && filename.match(config.testRegex)) {
  51. return false;
  52. }
  53. if (
  54. config.testMatch &&
  55. config.testMatch.length &&
  56. (_micromatch || _load_micromatch()).default.any(filename, config.testMatch)
  57. ) {
  58. return false;
  59. }
  60. if (
  61. // This configuration field contains an object in the form of:
  62. // {'path/to/file.js': true}
  63. options.collectCoverageOnlyFrom &&
  64. !options.collectCoverageOnlyFrom[filename]
  65. ) {
  66. return false;
  67. }
  68. if (
  69. // still cover if `only` is specified
  70. !options.collectCoverageOnlyFrom &&
  71. options.collectCoverageFrom &&
  72. !(0, (_micromatch || _load_micromatch()).default)(
  73. [(_path || _load_path()).default.relative(config.rootDir, filename)],
  74. options.collectCoverageFrom
  75. ).length
  76. ) {
  77. return false;
  78. }
  79. if (
  80. config.coveragePathIgnorePatterns &&
  81. config.coveragePathIgnorePatterns.some(pattern => filename.match(pattern))
  82. ) {
  83. return false;
  84. }
  85. if (MOCKS_PATTERN.test(filename)) {
  86. return false;
  87. }
  88. return true;
  89. }