UNPKG

4.52 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', {
4 value: true
5});
6exports.default = shouldInstrument;
7function path() {
8 const data = _interopRequireWildcard(require('path'));
9 path = function () {
10 return data;
11 };
12 return data;
13}
14function _micromatch() {
15 const data = _interopRequireDefault(require('micromatch'));
16 _micromatch = function () {
17 return data;
18 };
19 return data;
20}
21function _jestRegexUtil() {
22 const data = require('jest-regex-util');
23 _jestRegexUtil = function () {
24 return data;
25 };
26 return data;
27}
28function _jestUtil() {
29 const data = require('jest-util');
30 _jestUtil = function () {
31 return data;
32 };
33 return data;
34}
35function _interopRequireDefault(obj) {
36 return obj && obj.__esModule ? obj : {default: obj};
37}
38function _getRequireWildcardCache(nodeInterop) {
39 if (typeof WeakMap !== 'function') return null;
40 var cacheBabelInterop = new WeakMap();
41 var cacheNodeInterop = new WeakMap();
42 return (_getRequireWildcardCache = function (nodeInterop) {
43 return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
44 })(nodeInterop);
45}
46function _interopRequireWildcard(obj, nodeInterop) {
47 if (!nodeInterop && obj && obj.__esModule) {
48 return obj;
49 }
50 if (obj === null || (typeof obj !== 'object' && typeof obj !== 'function')) {
51 return {default: obj};
52 }
53 var cache = _getRequireWildcardCache(nodeInterop);
54 if (cache && cache.has(obj)) {
55 return cache.get(obj);
56 }
57 var newObj = {};
58 var hasPropertyDescriptor =
59 Object.defineProperty && Object.getOwnPropertyDescriptor;
60 for (var key in obj) {
61 if (key !== 'default' && Object.prototype.hasOwnProperty.call(obj, key)) {
62 var desc = hasPropertyDescriptor
63 ? Object.getOwnPropertyDescriptor(obj, key)
64 : null;
65 if (desc && (desc.get || desc.set)) {
66 Object.defineProperty(newObj, key, desc);
67 } else {
68 newObj[key] = obj[key];
69 }
70 }
71 }
72 newObj.default = obj;
73 if (cache) {
74 cache.set(obj, newObj);
75 }
76 return newObj;
77}
78/**
79 * Copyright (c) Meta Platforms, Inc. and affiliates.
80 *
81 * This source code is licensed under the MIT license found in the
82 * LICENSE file in the root directory of this source tree.
83 */
84
85const MOCKS_PATTERN = new RegExp(
86 (0, _jestRegexUtil().escapePathForRegex)(
87 `${path().sep}__mocks__${path().sep}`
88 )
89);
90const cachedRegexes = new Map();
91const getRegex = regexStr => {
92 if (!cachedRegexes.has(regexStr)) {
93 cachedRegexes.set(regexStr, new RegExp(regexStr));
94 }
95 const regex = cachedRegexes.get(regexStr);
96
97 // prevent stateful regexes from breaking, just in case
98 regex.lastIndex = 0;
99 return regex;
100};
101function shouldInstrument(filename, options, config, loadedFilenames) {
102 if (!options.collectCoverage) {
103 return false;
104 }
105 if (
106 config.forceCoverageMatch.length &&
107 _micromatch().default.any(filename, config.forceCoverageMatch)
108 ) {
109 return true;
110 }
111 if (
112 !config.testPathIgnorePatterns.some(pattern =>
113 getRegex(pattern).test(filename)
114 )
115 ) {
116 if (config.testRegex.some(regex => new RegExp(regex).test(filename))) {
117 return false;
118 }
119 if (
120 (0, _jestUtil().globsToMatcher)(config.testMatch)(
121 (0, _jestUtil().replacePathSepForGlob)(filename)
122 )
123 ) {
124 return false;
125 }
126 }
127 if (
128 options.collectCoverageFrom.length === 0 &&
129 loadedFilenames != null &&
130 !loadedFilenames.includes(filename)
131 ) {
132 return false;
133 }
134 if (
135 // still cover if `only` is specified
136 options.collectCoverageFrom.length &&
137 !(0, _jestUtil().globsToMatcher)(options.collectCoverageFrom)(
138 (0, _jestUtil().replacePathSepForGlob)(
139 path().relative(config.rootDir, filename)
140 )
141 )
142 ) {
143 return false;
144 }
145 if (
146 config.coveragePathIgnorePatterns.some(pattern => !!filename.match(pattern))
147 ) {
148 return false;
149 }
150 if (config.globalSetup === filename) {
151 return false;
152 }
153 if (config.globalTeardown === filename) {
154 return false;
155 }
156 if (config.setupFiles.includes(filename)) {
157 return false;
158 }
159 if (config.setupFilesAfterEnv.includes(filename)) {
160 return false;
161 }
162 if (MOCKS_PATTERN.test(filename)) {
163 return false;
164 }
165 if (options.changedFiles && !options.changedFiles.has(filename)) {
166 if (!options.sourcesRelatedToTestsInChangedFiles) {
167 return false;
168 }
169 if (!options.sourcesRelatedToTestsInChangedFiles.has(filename)) {
170 return false;
171 }
172 }
173 if (filename.endsWith('.json')) {
174 return false;
175 }
176 return true;
177}