should_instrument.js 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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) { return obj && obj.__esModule ? obj : { default: obj }; }
  19. const MOCKS_PATTERN = new RegExp((0, (_jestRegexUtil || _load_jestRegexUtil()).escapePathForRegex)((_path || _load_path()).default.sep + '__mocks__' + (_path || _load_path()).default.sep)); /**
  20. * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
  21. *
  22. * This source code is licensed under the MIT license found in the
  23. * LICENSE file in the root directory of this source tree.
  24. *
  25. *
  26. */
  27. function shouldInstrument(filename, options, config) {
  28. if (!options.collectCoverage) {
  29. return false;
  30. }
  31. if (config.forceCoverageMatch && config.forceCoverageMatch.length && (_micromatch || _load_micromatch()).default.any(filename, config.forceCoverageMatch)) {
  32. return true;
  33. }
  34. if (config.testRegex && filename.match(config.testRegex)) {
  35. return false;
  36. }
  37. if (config.testMatch && config.testMatch.length && (_micromatch || _load_micromatch()).default.any(filename, config.testMatch)) {
  38. return false;
  39. }
  40. if (
  41. // This configuration field contains an object in the form of:
  42. // {'path/to/file.js': true}
  43. options.collectCoverageOnlyFrom && !options.collectCoverageOnlyFrom[filename]) {
  44. return false;
  45. }
  46. if (
  47. // still cover if `only` is specified
  48. !options.collectCoverageOnlyFrom && options.collectCoverageFrom && !(0, (_micromatch || _load_micromatch()).default)([(_path || _load_path()).default.relative(config.rootDir, filename)], options.collectCoverageFrom).length) {
  49. return false;
  50. }
  51. if (config.coveragePathIgnorePatterns && config.coveragePathIgnorePatterns.some(pattern => filename.match(pattern))) {
  52. return false;
  53. }
  54. if (MOCKS_PATTERN.test(filename)) {
  55. return false;
  56. }
  57. return true;
  58. }