-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjest.setup.ts
55 lines (50 loc) · 1.56 KB
/
jest.setup.ts
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
/// <reference types="jest" />
import "@testing-library/jest-dom";
import "jest-extended";
import { checkAAAPattern } from "./src/utils/test-utils";
import fs from "fs/promises";
declare global {
namespace jest {
interface Matchers<R> {
toBeInTheDocument(): R;
toHaveTextContent(text: string): R;
toBeVisible(): R;
toBeDisabled(): R;
toBeEnabled(): R;
toHaveAttribute(attr: string, value?: string): R;
toHaveBeenCalledExactly(times: number): R;
}
}
}
function getTestPath(): string | undefined {
return (expect as any).getState().testPath;
}
beforeEach(async () => {
const testPath = getTestPath();
// Skip this check for test-rule.test.tsx since it contains intentionally invalid tests
// Also skip if we can't determine the test path
if (
testPath &&
!testPath.includes("test-rule.test.tsx") &&
!testPath.includes("node_modules")
) {
try {
const content = await fs.readFile(testPath, "utf8");
const result = checkAAAPattern(content);
if (!result.isValid) {
throw new Error(
`Test file is missing required AAA comments: ${result.missingComments.join(", ")}\n` +
"Each test should include:\n" +
"// Arrange - Set up test data and conditions\n" +
"// Act - Perform the action being tested\n" +
"// Assert - Verify the results",
);
}
} catch (error) {
if (error instanceof Error) {
throw new Error(`Failed to validate AAA pattern: ${error.message}`);
}
throw error;
}
}
});