@@ -17,122 +17,3 @@ To run a specific test file or directory:
1717npm test -- path/to/test/file.test.ts
1818npm test -- path/to/directory
1919```
20-
21- ## Writing Tests
22-
23- ### Basic Test Structure
24-
25- Each test file should follow this basic structure:
26-
27- ``` typescript
28- import { describe , it , expect , jest , beforeEach , afterEach } from ' @jest/globals' ;
29- import { moduleToTest } from ' ../path/to/module' ;
30-
31- describe (' Feature being tested' , () => {
32- beforeEach (() => {
33- // Setup before each test
34- jest .clearAllMocks ();
35- });
36-
37- afterEach (() => {
38- // Cleanup after each test
39- });
40-
41- it (' should do something specific' , () => {
42- // Test code
43- expect (result ).toBe (expectedValue );
44- });
45- });
46- ```
47-
48- ### Mocking
49-
50- #### Mocking Modules
51-
52- To mock an imported module:
53-
54- ``` typescript
55- jest .mock (' ../path/to/module' , () => {
56- return {
57- functionName: jest .fn ().mockReturnValue (' mocked value' ),
58- ClassName: jest .fn ().mockImplementation (() => ({
59- methodName: jest .fn ().mockResolvedValue (' result' ),
60- })),
61- };
62- });
63- ```
64-
65- #### Mocking HTTP Requests
66-
67- For testing API calls:
68-
69- ``` typescript
70- // Mock fetch or any HTTP client
71- global .fetch = jest .fn ().mockImplementation (() =>
72- Promise .resolve ({
73- ok: true ,
74- status: 200 ,
75- json : () => Promise .resolve ({ data: ' mock data' }),
76- })
77- );
78- ```
79-
80- ### Testing Async Code
81-
82- ``` typescript
83- it (' should handle async operations' , async () => {
84- const result = await asyncFunction ();
85- expect (result ).toEqual (expectedValue );
86- });
87-
88- it (' should handle rejected promises' , async () => {
89- await expect (failingAsyncFunction ()).rejects .toThrow ();
90- });
91- ```
92-
93- ### Testing MongoDB Tools
94-
95- When testing MongoDB tools, mock the MongoDB client:
96-
97- ``` typescript
98- jest .mock (' mongodb' , () => {
99- // Create mock implementation of MongoDB client
100- });
101- ```
102-
103- ### Testing Atlas API Tools
104-
105- When testing Atlas API tools, mock the authentication and HTTP calls:
106-
107- ``` typescript
108- // Mock authentication
109- jest .mock (' ../../src/common/atlas/auth' , () => ({
110- getAccessToken: jest .fn ().mockResolvedValue (' mock-token' ),
111- }));
112-
113- // Mock HTTP responses
114- global .fetch = jest .fn ();
115- (global .fetch as jest .Mock ).mockResolvedValue ({
116- ok: true ,
117- json : async () => ({ /* mock response data */ }),
118- });
119- ```
120-
121- ## Best Practices
122-
123- 1 . ** Test one thing per test** : Each test should verify one specific behavior
124- 2 . ** Use descriptive test names** : Test names should describe what is being tested
125- 3 . ** Mock external dependencies** : Don't rely on external services in unit tests
126- 4 . ** Clean up after tests** : Reset state after tests to avoid interference
127- 5 . ** Test edge cases** : Include tests for error conditions and edge cases
128- 6 . ** Keep tests independent** : Tests should not depend on each other
129-
130- ## ESM and TypeScript Configuration
131-
132- The project uses ESM modules with TypeScript. The Jest configuration in ` jest.config.js ` is set up to handle this properly. Make sure you:
133-
134- 1 . Use ` import ` instead of ` require `
135- 2 . Import from ` @jest/globals ` for Jest functions
136- 3 . Follow the patterns in the template test
137-
138- For more information on testing patterns and techniques, refer to the [ Jest documentation] ( https://jestjs.io/docs/getting-started ) .
0 commit comments