-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathenzyme-detector.ts
More file actions
61 lines (51 loc) · 2.03 KB
/
Copy pathenzyme-detector.ts
File metadata and controls
61 lines (51 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { existsSync, readdirSync, readFileSync, statSync } from "fs";
import { join } from "path";
import { yellow } from "ansi-colors";
import { widgetRoot } from "../widget/paths";
export function checkForEnzymeUsage(srcDir: string = "src"): void {
const srcPath = join(widgetRoot, srcDir);
if (!existsSync(srcPath)) {
return;
}
const enzymeFiles: string[] = [];
function scanDirectory(dir: string): void {
try {
const entries = readdirSync(dir);
for (const entry of entries) {
const fullPath = join(dir, entry);
const stat = statSync(fullPath);
if (stat.isDirectory()) {
scanDirectory(fullPath);
continue;
}
const isJsOrTsFile = /\.(jsx?|tsx?)$/.test(entry);
if (!stat.isFile() || !isJsOrTsFile) {
continue;
}
const content = readFileSync(fullPath, "utf8");
if (
/(from|require\s*\()\s*['"]enzyme['"]|enzyme.*(?:shallow|mount|render)|(?:shallow|mount|render).*enzyme/.test(
content
)
) {
enzymeFiles.push(fullPath.replace(widgetRoot, "."));
}
}
} catch (error) {
console.error(`Error scanning directory ${dir}:`, error);
}
}
scanDirectory(srcPath);
if (enzymeFiles.length > 0) {
console.log(yellow("\nWARNING: Enzyme usage detected in your tests"));
console.log(yellow("Enzyme is no longer supported. Please migrate your tests to React Testing Library."));
console.log(yellow("\nFiles with potential Enzyme usage:"));
enzymeFiles.forEach(file => console.log(yellow(` ${file}`)));
console.log(
yellow(
"\nFor migration guidance, see: https://testing-library.com/docs/react-testing-library/migrate-from-enzyme"
)
);
console.log();
}
}