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 for Unit Tests #37

Open
wants to merge 13 commits into
base: develop
Choose a base branch
from
Open
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
27 changes: 27 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Node.js CI

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
build:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [12.x, 14.x, 16.x]

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: npm ci
- run: npm run build --if-present
- name: Run Unit Tests
run: npm test
195 changes: 195 additions & 0 deletions test/influxdb-reporter.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
const InfluxDBReporter = require("../src/influxdb-reporter");
const HttpService = require("../src/http.service");
const UdpService = require("../src/udp.service");

jest.mock("../src/http.service");
jest.mock("../src/udp.service");

describe("InfluxDBReporter", () => {
let reporter;

beforeEach(() => {
reporter = new InfluxDBReporter();
});

describe("request", () => {
it("updates the current item's data correctly", () => {
const request = {
/* some request */
};
reporter.request(null, request);
expect(reporter.context.currentItem.data).toEqual(request);
});
});

beforeEach(() => {
reporter = new InfluxDBReporter();
});

describe("start", () => {
it("sets up the context correctly", () => {
const config = {
/* some configuration */
};
reporter.start(config);
expect(reporter.context).toEqual(config);
});

it("throws an error when required parameters are missing", () => {
const config = {
/* missing required parameters */
};
expect(() => reporter.start(config)).toThrow();
});
});

describe("beforeItem", () => {
it("updates the context correctly", () => {
const item = {
/* some item */
};
reporter.beforeItem(null, { item });
expect(reporter.context.currentItem).toBe(item);
});
});

describe("request", () => {
it("updates the current item's data correctly", () => {
const args = {
/* some args */
};
reporter.request(null, args);
expect(reporter.context.currentItem.data).toEqual(args);
});
});

describe("exception", () => {
it("does not throw any errors when called with an error", () => {
const error = new Error("Test error");
expect(() => reporter.exception(error)).not.toThrow();
});
});

describe("assertion", () => {
it("updates the assertion count correctly", () => {
reporter.assertion(null, {});
expect(reporter.context.currentItem.data.assertions).toBe(1);
});

it("handles failed and skipped assertions correctly", () => {
const error = {
/* some error */
};
reporter.assertion(error, { skipped: false });
expect(reporter.context.currentItem.data.failed_count).toBe(1);
expect(reporter.context.currentItem.data.skipped_count).toBe(0);

reporter.assertion(null, { skipped: true });
expect(reporter.context.currentItem.data.failed_count).toBe(1);
expect(reporter.context.currentItem.data.skipped_count).toBe(1);
});
});

describe("item", () => {
it("sends data to the service correctly", () => {
const item = {
/* some item */
};
reporter.context.currentItem = item;
const sendData = jest.spyOn(reporter.service, "sendData");
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.item();
expect(sendData).toHaveBeenCalledWith(item);
});
});

describe("done", () => {
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').

it("disconnects the service correctly", () => {
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').

const disconnect = jest.spyOn(reporter.service, "disconnect");
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.done();
expect(disconnect).toHaveBeenCalled();
});
});

beforeEach(() => {
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').

reporter = new InfluxDBReporter();
});

describe("start", () => {
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').

it("sets up the context correctly", () => {
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').

const config = {
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).

/* some configuration */
};
reporter.start(config);
expect(reporter.context).toEqual(config);
});

it("throws an error when required parameters are missing", () => {
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').

const config = {
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).

/* missing required parameters */
};
expect(() => reporter.start(config)).toThrow();
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').

});
});

describe("beforeItem", () => {
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').

it("updates the context correctly", () => {
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').

const item = {
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).

/* some item */
};
reporter.beforeItem(null, { item });
Copy link

Choose a reason for hiding this comment

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

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

expect(reporter.context.currentItem).toBe(item);
});
});

describe("request", () => {
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').
Too many errors. (73% scanned).

it("updates the current item's data correctly", () => {
const args = {
/* some args */
};
reporter.request(null, args);
expect(reporter.context.currentItem.data).toEqual(args);
});
});

describe("exception", () => {
it("does not throw any errors when called", () => {
expect(() => reporter.exception()).not.toThrow();
});
});

describe("assertion", () => {
it("updates the assertion count correctly", () => {
reporter.assertion(null, {});
expect(reporter.context.currentItem.data.assertions).toBe(1);
});

it("handles failed and skipped assertions correctly", () => {
const error = {
/* some error */
};
reporter.assertion(error, { skipped: false });
expect(reporter.context.currentItem.data.failed_count).toBe(1);
expect(reporter.context.currentItem.data.skipped_count).toBe(0);

reporter.assertion(null, { skipped: true });
expect(reporter.context.currentItem.data.failed_count).toBe(1);
expect(reporter.context.currentItem.data.skipped_count).toBe(1);
});
});

describe("item", () => {
it("sends data to the service correctly", () => {
const sendData = jest.spyOn(reporter.service, "sendData");
reporter.item();
expect(sendData).toHaveBeenCalled();
});
});

describe("done", () => {
it("disconnects the service correctly", () => {
const disconnect = jest.spyOn(reporter.service, "disconnect");
reporter.done();
expect(disconnect).toHaveBeenCalled();
});
});
});