Skip to content

Commit 4953300

Browse files
committed
feat: add contract testing with OpenAPI spec for Todos API
1 parent 17b4d72 commit 4953300

4 files changed

Lines changed: 125 additions & 14 deletions

File tree

.github/workflows/ci.yml

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,8 @@ jobs:
3030
env:
3131
CI: true
3232

33-
- name: Cache Puppeteer browsers
34-
uses: actions/cache@v4
35-
with:
36-
path: ~/.cache/puppeteer
37-
key: ${{ runner.os }}-puppeteer-${{ hashFiles('package-lock.json') }}
38-
restore-keys: |
39-
${{ runner.os }}-puppeteer-
40-
41-
- name: Install Chrome for Puppeteer
42-
run: npx puppeteer browsers install chrome
43-
4433
- name: Run TWD tests
45-
run: npx twd-cli run
34+
uses: BRIKEV/twd-cli/.github/actions/run@main
35+
with:
36+
contract-report: 'true'
4637

contracts/todos-3.0.json

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
{
2+
"openapi": "3.0.0",
3+
"info": {
4+
"title": "Todos API",
5+
"version": "1.0.0"
6+
},
7+
"paths": {
8+
"/todos": {
9+
"get": {
10+
"operationId": "getTodos",
11+
"responses": {
12+
"200": {
13+
"description": "List of todos",
14+
"content": {
15+
"application/json": {
16+
"schema": {
17+
"type": "array",
18+
"items": {
19+
"$ref": "#/components/schemas/Todo"
20+
}
21+
}
22+
}
23+
}
24+
}
25+
}
26+
},
27+
"post": {
28+
"operationId": "createTodo",
29+
"requestBody": {
30+
"required": true,
31+
"content": {
32+
"application/json": {
33+
"schema": {
34+
"$ref": "#/components/schemas/NewTodo"
35+
}
36+
}
37+
}
38+
},
39+
"responses": {
40+
"200": {
41+
"description": "Created todo",
42+
"content": {
43+
"application/json": {
44+
"schema": {
45+
"$ref": "#/components/schemas/Todo"
46+
}
47+
}
48+
}
49+
}
50+
}
51+
}
52+
},
53+
"/todos/{id}": {
54+
"delete": {
55+
"operationId": "deleteTodo",
56+
"parameters": [
57+
{
58+
"name": "id",
59+
"in": "path",
60+
"required": true,
61+
"schema": {
62+
"type": "string"
63+
}
64+
}
65+
],
66+
"responses": {
67+
"204": {
68+
"description": "Todo deleted"
69+
}
70+
}
71+
}
72+
}
73+
},
74+
"components": {
75+
"schemas": {
76+
"Todo": {
77+
"type": "object",
78+
"required": ["id", "title", "description", "date"],
79+
"properties": {
80+
"id": {
81+
"type": "string"
82+
},
83+
"title": {
84+
"type": "string"
85+
},
86+
"description": {
87+
"type": "string"
88+
},
89+
"date": {
90+
"type": "string"
91+
}
92+
}
93+
},
94+
"NewTodo": {
95+
"type": "object",
96+
"required": ["title", "description", "date"],
97+
"properties": {
98+
"title": {
99+
"type": "string"
100+
},
101+
"description": {
102+
"type": "string"
103+
},
104+
"date": {
105+
"type": "string"
106+
}
107+
}
108+
}
109+
}
110+
}
111+
}

src/twd-tests/todoList.twd.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ describe("Todo List Page", () => {
9494
method: "DELETE",
9595
url: "/api/todos/1",
9696
response: null,
97-
status: 200,
97+
status: 204,
9898
});
9999
await twd.mockRequest("getTodoList", {
100100
method: "GET",

twd.config.json

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,14 @@
55
"coverageDir": "./coverage",
66
"nycOutputDir": "./.nyc_output",
77
"headless": true,
8-
"puppeteerArgs": ["--no-sandbox", "--disable-setuid-sandbox"]
8+
"puppeteerArgs": ["--no-sandbox", "--disable-setuid-sandbox"],
9+
"contractReportPath": ".twd/contract-report.md",
10+
"contracts": [
11+
{
12+
"source": "./contracts/todos-3.0.json",
13+
"baseUrl": "/api",
14+
"mode": "error",
15+
"strict": true
16+
}
17+
]
918
}

0 commit comments

Comments
 (0)