Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit tests and GitHub Actions step to run them #49

Merged
merged 3 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: CI

on:
workflow_dispatch:
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');
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'const' is available in ES6 (use 'esversion: 6') or Mozilla JS extensions (use moz).

const InfluxDBReporter = require('../src/influxdb-reporter');
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'const' is available in ES6 (use 'esversion: 6') or Mozilla JS extensions (use moz).


describe('InfluxDBReporter', function() {
let reporter;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'let' is available in ES6 (use 'esversion: 6') or Mozilla JS extensions (use moz).

let newmanEmitter;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'let' is available in ES6 (use 'esversion: 6') or Mozilla JS extensions (use moz).

let reporterOptions;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'let' is available in ES6 (use 'esversion: 6') or Mozilla JS extensions (use moz).

let options;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'let' is available in ES6 (use 'esversion: 6') or Mozilla JS extensions (use moz).


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 = {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'const' is available in ES6 (use 'esversion: 6') or Mozilla JS extensions (use moz).

cursor: {},
item: { name: 'Test Item' },
request: { url: { toString: () => 'http://example.com' }, method: 'GET' },
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'arrow function syntax (=>)' is only available in ES6 (use 'esversion: 6').

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' };
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'const' is available in ES6 (use 'esversion: 6') or Mozilla JS extensions (use moz).

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' };
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'const' is available in ES6 (use 'esversion: 6') or Mozilla JS extensions (use moz).

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();
});
});
});