Skip to content

Commit 5914f2d

Browse files
authored
Merge pull request #1 from Nike-Inc/feat/add-vars-support
feat: add support for vars
2 parents d3051ae + e219ed2 commit 5914f2d

23 files changed

+1265
-311
lines changed

.github/WORKFLOW.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# CI/CD Workflow Documentation
2+
3+
## GitHub Actions Workflow Overview
4+
5+
The `publish.yml` workflow now has two distinct jobs that run based on different triggers:
6+
7+
### 🧪 **Test Job**
8+
9+
**Triggers:**
10+
11+
- Push to any branch targeting main
12+
- Pull requests to main branch
13+
14+
**What it does:**
15+
16+
- ✅ Runs on every push and PR
17+
- ✅ Installs dependencies
18+
- ✅ Executes test suite (`npm run test`)
19+
- ✅ Validates Bruno variable functionality
20+
- ✅ Ensures all assertions pass
21+
- ✅ Provides feedback on test results
22+
23+
### 📦 **Version & Publish Job**
24+
25+
**Triggers:**
26+
27+
- Only on pushes to main branch (after tests pass)
28+
- Never runs on PRs or other branches
29+
30+
**What it does:**
31+
32+
- ✅ Waits for test job to complete successfully (`needs: test`)
33+
- ✅ Bumps package version (patch)
34+
- ✅ Publishes to npm
35+
- ✅ Commits version bump back to repository
36+
37+
## Workflow Behavior Examples
38+
39+
### **Pull Request Scenario**
40+
41+
```
42+
PR created → Test job runs → ✅ Tests pass → PR can be merged
43+
PR created → Test job runs → ❌ Tests fail → PR blocked
44+
```
45+
46+
### **Push to Main Scenario**
47+
48+
```
49+
Push to main → Test job runs → ✅ Tests pass → Publish job runs → 📦 New version published
50+
Push to main → Test job runs → ❌ Tests fail → Publish job skipped → ❌ No publish
51+
```
52+
53+
### **Feature Branch Scenario**
54+
55+
```
56+
Push to feature-branch → Test job runs → ✅ Tests pass → No publish (safe)
57+
Push to feature-branch → Test job runs → ❌ Tests fail → Developer gets feedback
58+
```
59+
60+
## Benefits
61+
62+
1. **🛡️ Protection**: No broken code reaches npm
63+
2. **🔄 Continuous Feedback**: Tests run on every change
64+
3. **🚀 Automated Releases**: Successful main branch pushes auto-publish
65+
4. **👥 Team Safety**: PRs are validated before merge
66+
5. **📊 Quality Gates**: Clear pass/fail indicators
67+
68+
## Test Exit Codes
69+
70+
- **Exit 0**: All tests passed ✅
71+
- **Exit 1**: One or more tests failed ❌
72+
73+
## Commands
74+
75+
- `npm run test` - Run full test suite locally
76+
- `npm run test:ci` - Same as above (for CI clarity)
77+
- `npm run mock-server` - Start mock server manually

.github/workflows/publish.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,33 @@ on:
44
push:
55
branches:
66
- main
7+
pull_request:
8+
branches:
9+
- main
710

811
jobs:
12+
test:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- name: Checkout code
16+
uses: actions/checkout@v4
17+
18+
- name: Set up Node.js
19+
uses: actions/setup-node@v4
20+
with:
21+
node-version: "20.x"
22+
registry-url: "https://registry.npmjs.org/"
23+
24+
- name: Install dependencies
25+
run: yarn install --immutable
26+
27+
- name: Run tests
28+
run: npm run test
29+
930
version-and-publish:
1031
runs-on: ubuntu-latest
32+
needs: test
33+
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
1134
steps:
1235
- name: Checkout code
1336
uses: actions/checkout@v4

README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Chappie is a test runner for Bruno collections. The Bruno CLI runner doesn't all
1212
- Image comparison
1313
- JSON schema validation
1414
- Bruno response tests
15+
- **Bruno variable support** - Use `{{variable}}` syntax and `bru.setVar()` / `bru.getVar()`
1516

1617
## Usage
1718

@@ -39,6 +40,7 @@ chappie.run({
3940
actualImagesFolder: './actual-images', // where to store the image results
4041
baseImagesFolder'./base-images', // where to store the base images
4142
diffImagesFolder'./diff-images', // where to store the diff images (if they exist),
43+
allowCodeExecution: false, // set to true if the collection is trusted
4244
onTestError: error => {
4345
console.log("on test error: ", error.message);
4446
},
@@ -54,6 +56,48 @@ chappie.run({
5456
});
5557
```
5658

59+
### Bruno Variables
60+
61+
Chappie supports Bruno's variable system, allowing you to:
62+
63+
1. **Set variables in tests** using `bru.setVar(key, value)`
64+
2. **Use variables in URLs, headers, and request bodies** with `{{variableName}}` syntax
65+
3. **Access variables in tests** using `bru.getVar(key)`
66+
67+
**Example Bruno Collection with Variables:**
68+
69+
```json
70+
{
71+
"name": "Variable Example",
72+
"items": [
73+
{
74+
"name": "Set ID Variable",
75+
"seq": 1,
76+
"request": {
77+
"url": "https://jsonplaceholder.typicode.com/todos",
78+
"method": "GET",
79+
"tests": "const response = res.getBody();\nif (Array.isArray(response) && response.length > 0) {\n bru.setVar('ID', response[0].id);\n}"
80+
}
81+
},
82+
{
83+
"name": "Use ID Variable",
84+
"seq": 2,
85+
"request": {
86+
"url": "https://jsonplaceholder.typicode.com/todos/{{ID}}",
87+
"method": "GET",
88+
"tests": "test('Should get specific todo', function() {\n expect(res.getStatus()).to.equal(200);\n expect(res.getBody().id).to.equal(bru.getVar('ID'));\n});"
89+
}
90+
}
91+
]
92+
}
93+
```
94+
95+
**Variable Features:**
96+
97+
- Variables are **automatically interpolated** in URLs, headers, and request body values
98+
- Variables are **scoped per iteration** - they reset between iterations
99+
- **Security note:** Variable interpolation respects the `allowCodeExecution` setting
100+
57101
### Reports
58102

59103
Chappie also supports JUNIT tests.
@@ -73,4 +117,5 @@ chappie.run({
73117

74118
- ✅ Image comparison
75119
- ✅ JSON validation
120+
- ✅ Bruno variables and interpolation
76121
- ⬜️ Auth and token support

mock-data/README.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Mock Data Setup
2+
3+
This directory contains JSON files that are served by the mock server for local testing.
4+
5+
## File Structure
6+
7+
- `todos.json` - Array of all todos (served at `/todos`)
8+
- `todo-{id}.json` - Individual todo items (served at `/todos/{id}`)
9+
10+
## Usage
11+
12+
### 1. Start the mock server manually:
13+
14+
```bash
15+
npm run mock-server
16+
```
17+
18+
### 2. Run tests with the mock server:
19+
20+
```bash
21+
npm run test
22+
```
23+
24+
The test script automatically starts and stops the mock server.
25+
26+
## Available Endpoints
27+
28+
- `GET /todos` - Returns all todos from `todos.json`
29+
- `GET /todos/:id` - Returns specific todo from `todo-{id}.json`
30+
31+
## Customizing Responses
32+
33+
### Adding new todos:
34+
35+
1. Add the todo to the `todos.json` array
36+
2. Create a new `todo-{id}.json` file with the individual todo data
37+
38+
### Modifying existing responses:
39+
40+
1. Edit the appropriate JSON file
41+
2. Restart the server (if running manually)
42+
43+
## Example Files
44+
45+
- `todo-1.json` - Basic todo item
46+
- `todo-2.json` - Another basic todo item
47+
- `todo-3.json` - Incomplete todo
48+
- `todo-4.json` - Completed todo
49+
50+
## Testing Different Scenarios
51+
52+
You can create JSON files for different test scenarios:
53+
54+
- Error responses
55+
- Edge cases
56+
- Different data structures
57+
- Empty responses
58+
59+
Just create the appropriate JSON file and the mock server will serve it automatically.

mock-data/todo-1.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"userId": 1,
3+
"id": 1,
4+
"title": "delectus aut autem",
5+
"completed": false
6+
}

mock-data/todo-2.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"userId": 1,
3+
"id": 2,
4+
"title": "quis ut nam facilis et officia qui",
5+
"completed": false
6+
}

mock-data/todo-3.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"userId": 1,
3+
"id": 3,
4+
"title": "fugiat veniam minus",
5+
"completed": false
6+
}

mock-data/todo-4.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"userId": 2,
3+
"id": 4,
4+
"title": "et porro tempora",
5+
"completed": true
6+
}

mock-data/todos.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
[
2+
{
3+
"userId": 1,
4+
"id": 1,
5+
"title": "delectus aut autem",
6+
"completed": false
7+
},
8+
{
9+
"userId": 1,
10+
"id": 2,
11+
"title": "quis ut nam facilis et officia qui",
12+
"completed": false
13+
},
14+
{
15+
"userId": 1,
16+
"id": 3,
17+
"title": "fugiat veniam minus",
18+
"completed": false
19+
},
20+
{
21+
"userId": 1,
22+
"id": 4,
23+
"title": "et porro tempora",
24+
"completed": true
25+
},
26+
{
27+
"userId": 1,
28+
"id": 5,
29+
"title": "laboriosam mollitia et enim quasi adipisci quia provident illum",
30+
"completed": false
31+
}
32+
]

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
},
1111
"scripts": {
1212
"lint": "eslint ./src",
13-
"prettier:fix": "npx prettier --write ./src"
13+
"prettier:fix": "npx prettier --write ./src",
14+
"test": "node test/test.js",
15+
"mock-server": "node test/mock-server.js"
1416
},
1517
"files": [
1618
"src/**/*.js",

0 commit comments

Comments
 (0)