Skip to content

Commit cd01739

Browse files
committed
testing docs updated
1 parent 5879082 commit cd01739

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

docs/docs/docs/developer-resources/testing.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,64 @@ The `tests/server.ts` file exports the Talawa API server instance that can be im
5959

6060
There aren't any other strict structure requirements for the this directory.
6161

62+
# Mock GraphQL Context Factory Function
63+
In Directory `test/MockContext/*`
64+
65+
## **Purpose**
66+
The `createMockGraphQLContext` function provides a **fully mocked GraphQL context** for unit and integration testing of GraphQL resolvers. It ensures that resolvers can be tested **without needing a real database, MinIO storage, or authentication service** and works as centralised mocking mechanism.
67+
68+
## **Usage**
69+
70+
### **Importing the Mock Context**
71+
```ts
72+
import { createMockGraphQLContext } from "test/MockContext/mockGraphQLContext";
73+
```
74+
75+
### **Creating a Mock Context**
76+
#### **For an Unauthenticated User**
77+
```ts
78+
const mockContext = createMockGraphQLContext(false);
79+
```
80+
`mockContext.currentClient.isAuthenticated` will be `false`.
81+
82+
#### **For an Authenticated User**
83+
```ts
84+
const mockContext = createMockGraphQLContext(true, "user123");
85+
```
86+
`mockContext.currentClient.user.id` will be `"user123"`.
87+
88+
---
89+
90+
## **Components in Mock Context**
91+
The mock context provides the following:
92+
93+
| Component | Description |
94+
|----------------|------------|
95+
| `currentClient` | Simulates authenticated/unauthenticated users. |
96+
| `drizzleClient` | Mocked database client (`createMockDrizzleClient`). |
97+
| `envConfig` | Mocked environment variables (`API_BASE_URL`). |
98+
| `jwt.sign` | Mocked JWT generator (`vi.fn()` returning a test token). |
99+
| `log` | Mocked logger (`createMockLogger`). |
100+
| `minio` | Mocked MinIO client for object storage (`createMockMinioClient`). |
101+
| `pubsub` | Mocked pub-sub system for GraphQL subscriptions (`createMockPubSub`). |
102+
103+
---
104+
105+
## **How Contributors Should Use It**
106+
107+
### **Unit Testing Resolvers** (Mocks dependencies)
108+
```ts
109+
test("should return user data", async () => {
110+
const mockContext = createMockGraphQLContext(true, "user123");
111+
const result = await userResolver({}, {}, mockContext);
112+
expect(result.id).toBe("user123");
113+
});
114+
```
115+
116+
## **Key Benefits**
117+
**Isolated Testing** – No need for a real database or external services.
118+
**Scalable** – Any future changes in `GraphQLContext` can be updated in one place, ensuring a single source of truth.
119+
62120
### Future Considerations
63121

64122
In the future there might be a requirement to run some tests sequentially. When that moment arrives separating sequential and parallel tests into separate directories and using separate vitest configuration for them would be the best idea.

0 commit comments

Comments
 (0)