-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjest.setup.ts
83 lines (77 loc) · 2.52 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/// <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> {
// Testing Library matchers
toBeInTheDocument(): R;
toHaveTextContent(text: string): R;
toBeVisible(): R;
toBeDisabled(): R;
toBeEnabled(): R;
toHaveAttribute(attr: string, value?: string): R;
toHaveClass(...classNames: string[]): R;
toHaveStyle(css: Record<string, unknown>): R;
toContainElement(element: Element | null): R;
toContainHTML(html: string): R;
toHaveLength(length: number): R;
// Jest matchers
toBe(expected: any): R;
toEqual(expected: any): R;
toBeNull(): R;
toBeDefined(): R;
toBeUndefined(): R;
toBeTruthy(): R;
toBeFalsy(): R;
toBeGreaterThan(number: number): R;
toBeLessThan(number: number): R;
toBeLessThanOrEqual(number: number): R;
toBeGreaterThanOrEqual(number: number): R;
toContain(item: any): R;
toHaveBeenCalled(): R;
toHaveBeenCalledTimes(number: number): R;
toHaveBeenCalledWith(...args: any[]): R;
toHaveBeenCalledExactly(times: number): R;
toThrow(error?: string | Error | RegExp): R;
toMatch(regexpOrString: string | RegExp): R;
rejects: {
toThrow(error?: string | Error | RegExp): Promise<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;
}
}
});