@kanjijs/testing 0.2.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +47 -0
- package/dist/index.js +4630 -0
- package/package.json +20 -0
package/README.md
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# @kanjijs/testing
|
|
2
|
+
|
|
3
|
+
The official testing utility for Kanjijs Framework. It provides a robust way to test your Modules and Controllers by mocking dependencies in the `KanjijsIoC` container.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
bun add -d @kanjijs/testing
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
### Unit Testing Controllers
|
|
14
|
+
|
|
15
|
+
You can easily mock services to test controllers in isolation.
|
|
16
|
+
|
|
17
|
+
```typescript
|
|
18
|
+
import { Test } from "@kanjijs/testing";
|
|
19
|
+
import { UsersController } from "./users.controller";
|
|
20
|
+
import { UsersService } from "./users.service";
|
|
21
|
+
import { AppModule } from "./app.module";
|
|
22
|
+
|
|
23
|
+
describe("UsersController", () => {
|
|
24
|
+
it("should return mocked users", async () => {
|
|
25
|
+
// 1. Create a Testing Module
|
|
26
|
+
const moduleRef = await Test.createTestingModule({
|
|
27
|
+
imports: [AppModule], // Or just the module containing the controller
|
|
28
|
+
})
|
|
29
|
+
.overrideProvider(UsersService)
|
|
30
|
+
.useValue({
|
|
31
|
+
findAll: () => ["Test User 1", "Test User 2"],
|
|
32
|
+
})
|
|
33
|
+
.compile();
|
|
34
|
+
|
|
35
|
+
// 2. Get the Controller instance (dependencies are injected automatically)
|
|
36
|
+
const controller = moduleRef.get(UsersController);
|
|
37
|
+
|
|
38
|
+
// 3. Assert
|
|
39
|
+
expect(controller.findAll()).toEqual(["Test User 1", "Test User 2"]);
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### Key Features
|
|
45
|
+
- **`Test.createTestingModule(metadata)`**: Creates a sandbox for your test.
|
|
46
|
+
- **`.overrideProvider(token).useValue(mock)`**: Replaces a real provider with a mock implementation.
|
|
47
|
+
- **`KanjijsIoC` Integration**: Automatically resets the DI container between tests to prevent state leakage.
|