-
Notifications
You must be signed in to change notification settings - Fork 3
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
base: develop
Are you sure you want to change the base?
Changes from all commits
06e004c
c39c947
9c874d2
f2abc5b
2b5dc1d
fed46ae
2f05e6f
aa44015
f1611d5
2cd3401
2c2c458
3c33a96
74b7996
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
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"); | ||
reporter.item(); | ||
expect(sendData).toHaveBeenCalledWith(item); | ||
}); | ||
}); | ||
|
||
describe("done", () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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", () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(() => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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", () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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", () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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", () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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", () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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", () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 }); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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", () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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(); | ||
}); | ||
}); | ||
}); |
There was a problem hiding this comment.
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).