Skip to content

Commit 1e21281

Browse files
authored
Merge pull request #26 from seqeralabs/ci-testing-mocha
Add CI testing with mocha
2 parents eea2a43 + 0b5d5c4 commit 1e21281

19 files changed

+13567
-173
lines changed

.github/workflows/test.yml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
name: "Tests"
2+
3+
on:
4+
push:
5+
branches: [main]
6+
pull_request:
7+
8+
jobs:
9+
test:
10+
name: "Unit Tests"
11+
runs-on: ubuntu-latest
12+
strategy:
13+
matrix:
14+
node-version: [18.x, 20.x, 22.x]
15+
16+
steps:
17+
- name: Checkout repository
18+
uses: actions/checkout@v4
19+
20+
- name: Setup Node.js ${{ matrix.node-version }}
21+
uses: actions/setup-node@v4
22+
with:
23+
node-version: ${{ matrix.node-version }}
24+
cache: "npm"
25+
26+
- name: Install dependencies
27+
run: npm ci
28+
29+
- name: Run tests
30+
run: npm test
31+
32+
- name: Run tests with coverage
33+
if: matrix.node-version == '20.x'
34+
run: npm run test:ci
35+
36+
- name: Upload coverage to Codecov
37+
if: matrix.node-version == '20.x'
38+
uses: codecov/codecov-action@v4
39+
with:
40+
files: ./coverage/lcov.info
41+
fail_ci_if_error: false
42+
env:
43+
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ yarn-error.log*
1717

1818
# Build output / coverage
1919
coverage/
20+
.nyc_output/
2021
dist/
2122
build/
2223

CLAUDE.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ This is a Node-RED extension providing custom nodes for interacting with the Seq
1515
## Development Commands
1616

1717
```bash
18+
# Install dependencies
19+
npm install
20+
21+
# Run tests
22+
npm test
23+
24+
# Run tests with coverage
25+
npm run test:coverage
26+
1827
# Linting (uses pre-commit hooks)
1928
pre-commit run --all-files
2029

@@ -246,6 +255,9 @@ nodes/ - Node implementation files (.js + .html pairs)
246255
dataset-*.js - Dataset addition node
247256
datalink-*.js - Data Link list/poll nodes
248257
studios-*.js - Studio addition/monitor nodes
258+
test/ - Mocha test files
259+
helper.js - Shared test utilities and mock factories
260+
*_spec.js - Test files for each node
249261
examples/ - Example flows (.json)
250262
docker/ - Dockerfiles and Node-RED config for containers
251263
docs/ - Documentation and images
@@ -263,3 +275,4 @@ docs/ - Documentation and images
263275
7. Use `node.status()` to update visual state in editor
264276
8. Register node in [package.json](package.json) under `node-red.nodes`
265277
9. Create corresponding HTML with `<script type="text/html" data-template-name="...">` for editor UI
278+
10. Add tests in `test/<node-name>_spec.js` using shared helpers from `test/helper.js`

CONTRIBUTING.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,53 @@ After making code changes:
8686
- **JavaScript files (`.js`)**: Restart Node-RED
8787
- **HTML files (`.html`)**: Refresh your browser (Ctrl+Shift+R / Cmd+Shift+R)
8888

89+
## Running Tests
90+
91+
This project uses [Mocha](https://mochajs.org/) for testing with [node-red-node-test-helper](https://github.com/node-red/node-red-node-test-helper) for Node-RED integration testing.
92+
93+
### Install Dependencies
94+
95+
```bash
96+
npm install
97+
```
98+
99+
### Run Tests
100+
101+
```bash
102+
# Run all tests
103+
npm test
104+
105+
# Run tests with coverage report
106+
npm run test:coverage
107+
108+
# Run tests with lcov output (for CI)
109+
npm run test:ci
110+
```
111+
112+
### Test Structure
113+
114+
Tests are located in the `test/` directory:
115+
116+
- `test/helper.js` - Shared test utilities and mock factories
117+
- `test/*_spec.js` - Test files for each node (following Node-RED naming convention)
118+
119+
Each test file covers:
120+
121+
- Node loading and configuration
122+
- Input message handling
123+
- API call mocking (using [nock](https://github.com/nock/nock))
124+
- Output message verification
125+
- Error handling
126+
127+
### Writing Tests
128+
129+
When adding new nodes or modifying existing ones, please add or update tests accordingly. Tests should:
130+
131+
1. Use the shared helper functions from `test/helper.js`
132+
2. Mock all external API calls with nock
133+
3. Test both success and error paths
134+
4. Verify message passthrough (custom properties preserved)
135+
89136
## Docker Development
90137

91138
The project has two Docker images includeed, that bundle and customise a Node-RED installation and come with the Seqera nodes pre-installed.

nodes/datalink-poll.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,15 +107,18 @@ module.exports = function (RED) {
107107
};
108108

109109
// Start the polling interval
110+
let intervalId = null;
110111
if (node.seqeraConfig && config.dataLinkName && config.dataLinkName.trim() !== "") {
111112
const intervalMs = node.pollFrequencySec * 1000;
112-
const intervalId = setInterval(executePoll, intervalMs);
113+
intervalId = setInterval(executePoll, intervalMs);
113114
// run once immediately
114115
executePoll();
115116
}
116117

117118
node.on("close", () => {
118-
clearInterval(intervalId);
119+
if (intervalId) {
120+
clearInterval(intervalId);
121+
}
119122
});
120123
}
121124

0 commit comments

Comments
 (0)