Skip to content

Commit

Permalink
Add unit tests and GitHub Actions step to run them
Browse files Browse the repository at this point in the history
Related to #35

Add unit tests and GitHub Actions workflow for `influxdb-reporter`.

* **Unit Tests**
  - Create `test/influxdb-reporter.test.js` to test `InfluxDBReporter` class.
  - Test initialization, `start`, `beforeItem`, `request`, `assertion`, `item`, and `done` methods.
  - Mock necessary objects and functions for testing.

* **GitHub Actions Workflow**
  - Add `.github/workflows/main.yml` to define CI workflow.
  - Include steps to checkout repository, set up Node.js, install dependencies, build, and run unit tests.

* **package.json**
  - Update `scripts` section to include `"test": "jest"`.
  - Add `jest` as a dev dependency.
  • Loading branch information
vs4vijay committed Aug 1, 2024
1 parent 58a9208 commit 0d0ddd2
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 1 deletion.
31 changes: 31 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: CI

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v2

- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: '14'

- name: Install dependencies
run: npm install

- name: Build
run: npm run build

- name: Run unit tests
run: npm test
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Newman Reporter for InfluxDB",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "jest"
},
"repository": {
"type": "git",
Expand All @@ -28,5 +28,8 @@
"homepage": "https://github.com/vs4vijay/newman-reporter-influxdb#readme",
"dependencies": {
"axios": "^0.21.1"
},
"devDependencies": {
"jest": "^26.6.3"
}
}
104 changes: 104 additions & 0 deletions test/influxdb-reporter.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
const assert = require('assert');
const InfluxDBReporter = require('../src/influxdb-reporter');

describe('InfluxDBReporter', function() {
let reporter;
let newmanEmitter;
let reporterOptions;
let options;

beforeEach(function() {
newmanEmitter = {
on: function(event, callback) {
// Mock implementation of newmanEmitter.on
}
};
reporterOptions = {
influxdbServer: 'localhost',
influxdbPort: 8086,
influxdbName: 'test_db',
influxdbMeasurement: 'test_measurement'
};
options = {
collection: {
name: 'Test Collection'
}
};
reporter = new InfluxDBReporter(newmanEmitter, reporterOptions, options);
});

describe('start', function() {
it('should initialize context and service', function() {
reporter.start();
assert.strictEqual(reporter.context.server, 'localhost');
assert.strictEqual(reporter.context.port, 8086);
assert.strictEqual(reporter.context.name, 'test_db');
assert.strictEqual(reporter.context.measurement, 'test_measurement');
});
});

describe('beforeItem', function() {
it('should update currentItem and list', function() {
reporter.beforeItem();
assert.strictEqual(reporter.context.currentItem.index, 1);
assert.strictEqual(reporter.context.list.length, 1);
});
});

describe('request', function() {
it('should update currentItem data', function() {
const args = {
cursor: {},
item: { name: 'Test Item' },
request: { url: { toString: () => 'http://example.com' }, method: 'GET' },
response: { status: 'OK', code: 200, responseTime: 100, responseSize: 1000 }
};
reporter.request(null, args);
assert.strictEqual(reporter.context.currentItem.data.request_name, 'Test Item');
assert.strictEqual(reporter.context.currentItem.data.url, 'http://example.com');
assert.strictEqual(reporter.context.currentItem.data.method, 'GET');
assert.strictEqual(reporter.context.currentItem.data.status, 'OK');
assert.strictEqual(reporter.context.currentItem.data.code, 200);
assert.strictEqual(reporter.context.currentItem.data.response_time, 100);
assert.strictEqual(reporter.context.currentItem.data.response_size, 1000);
});
});

describe('assertion', function() {
it('should update currentItem data for failed assertion', function() {
const error = { test: 'Test Assertion', name: 'AssertionError', message: 'Test failed' };
reporter.assertion(error);
assert.strictEqual(reporter.context.currentItem.data.test_status, 'FAIL');
assert.strictEqual(reporter.context.currentItem.data.failed.length, 1);
});

it('should update currentItem data for skipped assertion', function() {
const args = { skipped: true, assertion: 'Test Assertion' };
reporter.assertion(null, args);
assert.strictEqual(reporter.context.currentItem.data.test_status, 'SKIP');
assert.strictEqual(reporter.context.currentItem.data.skipped.length, 1);
});
});

describe('item', function() {
it('should send data to service', function() {
reporter.service = {
sendData: function(data) {
assert.ok(data.includes('test_measurement'));
}
};
reporter.item();
});
});

describe('done', function() {
it('should disconnect service', function() {
reporter.service = {
disconnect: function() {
assert.ok(true);
}
};
reporter.done();
});
});
});

0 comments on commit 0d0ddd2

Please sign in to comment.