|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- 'use strict';
-
- Object.defineProperty(exports, '__esModule', {
- value: true
- });
- exports.default = shouldInstrument;
-
- function path() {
- const data = _interopRequireWildcard(require('path'));
-
- path = function () {
- return data;
- };
-
- return data;
- }
-
- function _micromatch() {
- const data = _interopRequireDefault(require('micromatch'));
-
- _micromatch = function () {
- return data;
- };
-
- return data;
- }
-
- function _jestRegexUtil() {
- const data = require('jest-regex-util');
-
- _jestRegexUtil = function () {
- return data;
- };
-
- return data;
- }
-
- function _jestUtil() {
- const data = require('jest-util');
-
- _jestUtil = function () {
- return data;
- };
-
- return data;
- }
-
- function _interopRequireDefault(obj) {
- return obj && obj.__esModule ? obj : {default: obj};
- }
-
- function _getRequireWildcardCache(nodeInterop) {
- if (typeof WeakMap !== 'function') return null;
- var cacheBabelInterop = new WeakMap();
- var cacheNodeInterop = new WeakMap();
- return (_getRequireWildcardCache = function (nodeInterop) {
- return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
- })(nodeInterop);
- }
-
- function _interopRequireWildcard(obj, nodeInterop) {
- if (!nodeInterop && obj && obj.__esModule) {
- return obj;
- }
- if (obj === null || (typeof obj !== 'object' && typeof obj !== 'function')) {
- return {default: obj};
- }
- var cache = _getRequireWildcardCache(nodeInterop);
- if (cache && cache.has(obj)) {
- return cache.get(obj);
- }
- var newObj = {};
- var hasPropertyDescriptor =
- Object.defineProperty && Object.getOwnPropertyDescriptor;
- for (var key in obj) {
- if (key !== 'default' && Object.prototype.hasOwnProperty.call(obj, key)) {
- var desc = hasPropertyDescriptor
- ? Object.getOwnPropertyDescriptor(obj, key)
- : null;
- if (desc && (desc.get || desc.set)) {
- Object.defineProperty(newObj, key, desc);
- } else {
- newObj[key] = obj[key];
- }
- }
- }
- newObj.default = obj;
- if (cache) {
- cache.set(obj, newObj);
- }
- return newObj;
- }
-
- /**
- * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
- *
- * This source code is licensed under the MIT license found in the
- * LICENSE file in the root directory of this source tree.
- */
- const MOCKS_PATTERN = new RegExp(
- (0, _jestRegexUtil().escapePathForRegex)(
- path().sep + '__mocks__' + path().sep
- )
- );
- const cachedRegexes = new Map();
-
- const getRegex = regexStr => {
- if (!cachedRegexes.has(regexStr)) {
- cachedRegexes.set(regexStr, new RegExp(regexStr));
- }
-
- const regex = cachedRegexes.get(regexStr); // prevent stateful regexes from breaking, just in case
-
- regex.lastIndex = 0;
- return regex;
- };
-
- function shouldInstrument(filename, options, config) {
- if (!options.collectCoverage) {
- return false;
- }
-
- if (
- config.forceCoverageMatch.length &&
- _micromatch().default.any(filename, config.forceCoverageMatch)
- ) {
- return true;
- }
-
- if (
- !config.testPathIgnorePatterns.some(pattern =>
- getRegex(pattern).test(filename)
- )
- ) {
- if (config.testRegex.some(regex => new RegExp(regex).test(filename))) {
- return false;
- }
-
- if (
- (0, _jestUtil().globsToMatcher)(config.testMatch)(
- (0, _jestUtil().replacePathSepForGlob)(filename)
- )
- ) {
- return false;
- }
- }
-
- if (
- // This configuration field contains an object in the form of:
- // {'path/to/file.js': true}
- options.collectCoverageOnlyFrom &&
- !options.collectCoverageOnlyFrom[filename]
- ) {
- return false;
- }
-
- if (
- // still cover if `only` is specified
- !options.collectCoverageOnlyFrom &&
- options.collectCoverageFrom.length &&
- !(0, _jestUtil().globsToMatcher)(options.collectCoverageFrom)(
- (0, _jestUtil().replacePathSepForGlob)(
- path().relative(config.rootDir, filename)
- )
- )
- ) {
- return false;
- }
-
- if (
- config.coveragePathIgnorePatterns.some(pattern => !!filename.match(pattern))
- ) {
- return false;
- }
-
- if (config.globalSetup === filename) {
- return false;
- }
-
- if (config.globalTeardown === filename) {
- return false;
- }
-
- if (config.setupFiles.includes(filename)) {
- return false;
- }
-
- if (config.setupFilesAfterEnv.includes(filename)) {
- return false;
- }
-
- if (MOCKS_PATTERN.test(filename)) {
- return false;
- }
-
- if (options.changedFiles && !options.changedFiles.has(filename)) {
- if (!options.sourcesRelatedToTestsInChangedFiles) {
- return false;
- }
-
- if (!options.sourcesRelatedToTestsInChangedFiles.has(filename)) {
- return false;
- }
- }
-
- return true;
- }
|