Skip to content

Commit 14be7cb

Browse files
committed
chore: updates to readme and templates
1 parent 6e79db2 commit 14be7cb

File tree

4 files changed

+11
-26
lines changed

4 files changed

+11
-26
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,9 @@ pactflow-ai generate \
107107
--code ./src/product.js \
108108
--code ./src/api.js \
109109
--template ./src/pact.test.template \
110-
--instructions ./src/test.instructions.txt
110+
--instructions @./src/test.instructions.txt
111111
```
112112

113113
### Running Tests
114114

115-
* `npm run test:pact`
115+
* `npm t`

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"axios": "^0.27.2"
77
},
88
"scripts": {
9-
"test:pact": "cross-env CI=true npx jest --colors --testTimeout 30000 --testMatch \"**/*.pact.spec.ts\""
9+
"test": "cross-env CI=true npx jest --colors --testTimeout 30000 --testMatch \"**/*.pact.spec.ts\""
1010
},
1111
"devDependencies": {
1212
"@babel/core": "^7.25.2",

src/pact.test.template

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,8 @@ import { ThingAPI } from './thing'
44
// Extract matchers here to improve readability when used in the test
55
const { like } = MatchersV3;
66

7-
// Create a 3 level test hierarchy
8-
//
9-
// 1. Top level describe block containing the name of the API being tested
10-
// 2. Describe block for the specific API endpoint
11-
// 3. Test block for the specific test case
12-
// 4. Execute the test case
13-
// 5. Call the API under test
14-
// 6. Assert the response
15-
// 8. Use Pact matchers to constrain and test the Provider response
16-
// 7. Use Jest matchers to assert the API client behaviour
17-
187
// Top level - name of the API
19-
describe("Thing API", () => {
8+
describe("🧱 Thing API", () => {
209
// Use the PactV4 class, and serialise the Pact as V4 Pact Specification
2110
const pact = new PactV4({
2211
consumer: "ThingConsumer",
@@ -25,20 +14,18 @@ describe("Thing API", () => {
2514
});
2615

2716
// Level 2 - Describe block for the specific API endpoint
28-
describe("GET /thing/:id", () => {
17+
describe("🔌 GET /thing/:id", () => {
2918

3019
// Level 3 - Test block for the specific test case
31-
test("given a valid thing, returns 200", async () => {
20+
test("🧪 given a valid thing, returns 200", async () => {
3221
await pact
3322
.addInteraction()
3423
.given("a thing with id 1 exists")
3524
.uponReceiving("a request for a valid thing")
36-
// Avoid matchers on the request unless necessary
3725
.withRequest("GET", "/thing/1", (builder) => {
3826
builder.headers({ Accept: "application/json" });
3927
})
4028
.willRespondWith(200, (builder) => {
41-
// Use loose matchers where possible, to avoid unnecessary constraints on the provider
4229
builder.jsonBody(
4330
like({
4431
id: 1,
@@ -48,13 +35,10 @@ describe("Thing API", () => {
4835
);
4936
})
5037
.executeTest(async (mockserver) => {
51-
// Instantiate the ThingAPI client
5238
const ThingAPI = new ThingAPI(mockserver.url);
5339

54-
// Call the API under test
5540
const Thing = await ThingAPI.getThingById(1);
5641

57-
// Use Jest matchers to assert the response
5842
expect(Thing).toEqual({
5943
id: 1,
6044
name: "Some 1",

src/test.instructions.txt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
* Make sure to cover happy and non-happy paths
2+
* Specifically, ensure to include test cases for the positive (HTTP 200) scenario and negative scenarios, specifically the case of 400, 401 and 404
13
* Use the Jest testing framework
24
* Use the native Jest expect (https://jestjs.io/docs/expect) matchers such as `toEqual` and `toBeTruthy`
35
* Prefer the use of the async/await pattern when using Promises
46
* Use the PactV4 interface
57
* Use a 3 level hierarchy for tests
6-
1. Level 1 should be the API under test as a `describe` block e.g. "Product API"
7-
2. Level 2 should be the endpoint as a `describe` block e.g. "GET /products/:id", "POST /products"
8-
3. Level 3 should be the scenario as a `test` block e.g. "Given a valid user, returns a 200", "Given an invalid user, returns a 400"
9-
* Write test cases for the positive (HTTP 200) scenario and negative scenarios, specifically the case of 400, 401 and 404
8+
1. Level 1 should be the API under test as a `describe` block e.g. "🧱 Product API"
9+
2. Level 2 should be the endpoint as a `describe` block e.g. "🔌 GET /products/:id", "POST /products"
10+
3. Level 3 should be the scenario as a `test` block e.g. "🧪 Given a valid user, returns a 200", "Given an invalid user, returns a 400"

0 commit comments

Comments
 (0)