fix(test): Use 'npm run lint-fix' to fix 'mocha/no-mocha-arrows' like lint warnings
- ref: https://mochajs.org/#arrow-functions
This commit is contained in:
1 parent
319e6d6017
commit
fcda60fe5b
31 files changed
+1268
-907
No files matched your search
@@ -3,10 +3,10 @@ import NetworkService from "../../service/networkService.js";
|
||||
import { expect } from "chai";
|
||||
import http from "http";
|
||||
import { errorMessages } from "../../utils/messages.js";
|
||||
describe("Network Service", () => {
|
||||
describe("Network Service", function() {
|
||||
let axios, ping, Docker, logger, networkService;
|
||||
|
||||
beforeEach(() => {
|
||||
beforeEach(function() {
|
||||
axios = {
|
||||
get: sinon.stub().resolves({
|
||||
data: { foo: "bar" },
|
||||
@@ -34,21 +34,23 @@ describe("Network Service", () => {
|
||||
logger = { error: sinon.stub() };
|
||||
networkService = new NetworkService(axios, ping, logger, http, Docker);
|
||||
});
|
||||
describe("constructor", () => {
|
||||
it("should create a new NetworkService instance", () => {
|
||||
|
||||
describe("constructor", function() {
|
||||
it("should create a new NetworkService instance", function() {
|
||||
const networkService = new NetworkService();
|
||||
expect(networkService).to.be.an.instanceOf(NetworkService);
|
||||
});
|
||||
});
|
||||
|
||||
describe("timeRequest", () => {
|
||||
it("should time an asynchronous operation", async () => {
|
||||
describe("timeRequest", function() {
|
||||
it("should time an asynchronous operation", async function() {
|
||||
const operation = sinon.stub().resolves("success");
|
||||
const { response, responseTime } = await networkService.timeRequest(operation);
|
||||
expect(response).to.equal("success");
|
||||
expect(responseTime).to.be.a("number");
|
||||
});
|
||||
it("should handle errors if operation throws error", async () => {
|
||||
|
||||
it("should handle errors if operation throws error", async function() {
|
||||
const error = new Error("Test error");
|
||||
const operation = sinon.stub().throws(error);
|
||||
const { response, responseTime } = await networkService.timeRequest(operation);
|
||||
@@ -58,8 +60,8 @@ describe("Network Service", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("requestPing", () => {
|
||||
it("should return a response object if ping successful", async () => {
|
||||
describe("requestPing", function() {
|
||||
it("should return a response object if ping successful", async function() {
|
||||
const pingResult = await networkService.requestPing({
|
||||
data: { url: "http://test.com", _id: "123" },
|
||||
});
|
||||
@@ -68,7 +70,8 @@ describe("Network Service", () => {
|
||||
expect(pingResult.responseTime).to.be.a("number");
|
||||
expect(pingResult.status).to.be.true;
|
||||
});
|
||||
it("should return a response object if ping unsuccessful", async () => {
|
||||
|
||||
it("should return a response object if ping unsuccessful", async function() {
|
||||
const error = new Error("Test error");
|
||||
networkService.timeRequest = sinon
|
||||
.stub()
|
||||
@@ -82,7 +85,8 @@ describe("Network Service", () => {
|
||||
expect(pingResult.status).to.be.false;
|
||||
expect(pingResult.code).to.equal(networkService.PING_ERROR);
|
||||
});
|
||||
it("should throw an error if ping cannot resolve", async () => {
|
||||
|
||||
it("should throw an error if ping cannot resolve", async function() {
|
||||
const error = new Error("test error");
|
||||
networkService.timeRequest = sinon.stub().throws(error);
|
||||
try {
|
||||
@@ -95,8 +99,9 @@ describe("Network Service", () => {
|
||||
}
|
||||
});
|
||||
});
|
||||
describe("requestHttp", () => {
|
||||
it("should return a response object if http successful", async () => {
|
||||
|
||||
describe("requestHttp", function() {
|
||||
it("should return a response object if http successful", async function() {
|
||||
const job = { data: { url: "http://test.com", _id: "123", type: "http" } };
|
||||
const httpResult = await networkService.requestHttp(job);
|
||||
expect(httpResult.monitorId).to.equal("123");
|
||||
@@ -104,7 +109,8 @@ describe("Network Service", () => {
|
||||
expect(httpResult.responseTime).to.be.a("number");
|
||||
expect(httpResult.status).to.be.true;
|
||||
});
|
||||
it("should return a response object if http unsuccessful", async () => {
|
||||
|
||||
it("should return a response object if http unsuccessful", async function() {
|
||||
const error = new Error("Test error");
|
||||
error.response = { status: 404 };
|
||||
networkService.timeRequest = sinon
|
||||
@@ -118,7 +124,8 @@ describe("Network Service", () => {
|
||||
expect(httpResult.status).to.be.false;
|
||||
expect(httpResult.code).to.equal(404);
|
||||
});
|
||||
it("should return a response object if http unsuccessful with unknown code", async () => {
|
||||
|
||||
it("should return a response object if http unsuccessful with unknown code", async function() {
|
||||
const error = new Error("Test error");
|
||||
error.response = {};
|
||||
networkService.timeRequest = sinon
|
||||
@@ -132,7 +139,8 @@ describe("Network Service", () => {
|
||||
expect(httpResult.status).to.be.false;
|
||||
expect(httpResult.code).to.equal(networkService.NETWORK_ERROR);
|
||||
});
|
||||
it("should throw an error if an error occurs", async () => {
|
||||
|
||||
it("should throw an error if an error occurs", async function() {
|
||||
const error = new Error("test error");
|
||||
networkService.timeRequest = sinon.stub().throws(error);
|
||||
try {
|
||||
@@ -146,8 +154,8 @@ describe("Network Service", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("requestPagespeed", () => {
|
||||
it("should return a response object if pagespeed successful", async () => {
|
||||
describe("requestPagespeed", function() {
|
||||
it("should return a response object if pagespeed successful", async function() {
|
||||
const job = { data: { url: "http://test.com", _id: "123", type: "pagespeed" } };
|
||||
const pagespeedResult = await networkService.requestPagespeed(job);
|
||||
expect(pagespeedResult.monitorId).to.equal("123");
|
||||
@@ -155,7 +163,8 @@ describe("Network Service", () => {
|
||||
expect(pagespeedResult.responseTime).to.be.a("number");
|
||||
expect(pagespeedResult.status).to.be.true;
|
||||
});
|
||||
it("should return a response object if pagespeed unsuccessful", async () => {
|
||||
|
||||
it("should return a response object if pagespeed unsuccessful", async function() {
|
||||
const error = new Error("Test error");
|
||||
error.response = { status: 404 };
|
||||
networkService.timeRequest = sinon
|
||||
@@ -169,7 +178,8 @@ describe("Network Service", () => {
|
||||
expect(pagespeedResult.status).to.be.false;
|
||||
expect(pagespeedResult.code).to.equal(404);
|
||||
});
|
||||
it("should return a response object if pagespeed unsuccessful with an unknown code", async () => {
|
||||
|
||||
it("should return a response object if pagespeed unsuccessful with an unknown code", async function() {
|
||||
const error = new Error("Test error");
|
||||
error.response = {};
|
||||
networkService.timeRequest = sinon
|
||||
@@ -183,7 +193,8 @@ describe("Network Service", () => {
|
||||
expect(pagespeedResult.status).to.be.false;
|
||||
expect(pagespeedResult.code).to.equal(networkService.NETWORK_ERROR);
|
||||
});
|
||||
it("should throw an error if pagespeed cannot resolve", async () => {
|
||||
|
||||
it("should throw an error if pagespeed cannot resolve", async function() {
|
||||
const error = new Error("test error");
|
||||
networkService.timeRequest = sinon.stub().throws(error);
|
||||
try {
|
||||
@@ -197,8 +208,8 @@ describe("Network Service", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("requestHardware", () => {
|
||||
it("should return a response object if hardware successful", async () => {
|
||||
describe("requestHardware", function() {
|
||||
it("should return a response object if hardware successful", async function() {
|
||||
const job = { data: { url: "http://test.com", _id: "123", type: "hardware" } };
|
||||
const httpResult = await networkService.requestHardware(job);
|
||||
expect(httpResult.monitorId).to.equal("123");
|
||||
@@ -206,7 +217,8 @@ describe("Network Service", () => {
|
||||
expect(httpResult.responseTime).to.be.a("number");
|
||||
expect(httpResult.status).to.be.true;
|
||||
});
|
||||
it("should return a response object if hardware successful and job has a secret", async () => {
|
||||
|
||||
it("should return a response object if hardware successful and job has a secret", async function() {
|
||||
const job = {
|
||||
data: {
|
||||
url: "http://test.com",
|
||||
@@ -221,7 +233,8 @@ describe("Network Service", () => {
|
||||
expect(httpResult.responseTime).to.be.a("number");
|
||||
expect(httpResult.status).to.be.true;
|
||||
});
|
||||
it("should return a response object if hardware unsuccessful", async () => {
|
||||
|
||||
it("should return a response object if hardware unsuccessful", async function() {
|
||||
const error = new Error("Test error");
|
||||
error.response = { status: 404 };
|
||||
networkService.timeRequest = sinon
|
||||
@@ -235,7 +248,8 @@ describe("Network Service", () => {
|
||||
expect(httpResult.status).to.be.false;
|
||||
expect(httpResult.code).to.equal(404);
|
||||
});
|
||||
it("should return a response object if hardware unsuccessful with unknown code", async () => {
|
||||
|
||||
it("should return a response object if hardware unsuccessful with unknown code", async function() {
|
||||
const error = new Error("Test error");
|
||||
error.response = {};
|
||||
networkService.timeRequest = sinon
|
||||
@@ -249,7 +263,8 @@ describe("Network Service", () => {
|
||||
expect(httpResult.status).to.be.false;
|
||||
expect(httpResult.code).to.equal(networkService.NETWORK_ERROR);
|
||||
});
|
||||
it("should throw an error if hardware cannot resolve", async () => {
|
||||
|
||||
it("should throw an error if hardware cannot resolve", async function() {
|
||||
const error = new Error("test error");
|
||||
networkService.timeRequest = sinon.stub().throws(error);
|
||||
try {
|
||||
@@ -263,8 +278,8 @@ describe("Network Service", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("requestDocker", () => {
|
||||
it("should return a response object if docker successful", async () => {
|
||||
describe("requestDocker", function() {
|
||||
it("should return a response object if docker successful", async function() {
|
||||
const job = { data: { url: "http://test.com", _id: "123", type: "docker" } };
|
||||
const dockerResult = await networkService.requestDocker(job);
|
||||
expect(dockerResult.monitorId).to.equal("123");
|
||||
@@ -273,7 +288,7 @@ describe("Network Service", () => {
|
||||
expect(dockerResult.status).to.be.true;
|
||||
});
|
||||
|
||||
it("should return a response object with status false if container not running", async () => {
|
||||
it("should return a response object with status false if container not running", async function() {
|
||||
Docker = class {
|
||||
listContainers = sinon.stub().resolves([
|
||||
{
|
||||
@@ -292,7 +307,7 @@ describe("Network Service", () => {
|
||||
expect(dockerResult.code).to.equal(200);
|
||||
});
|
||||
|
||||
it("should handle an error when fetching the container", async () => {
|
||||
it("should handle an error when fetching the container", async function() {
|
||||
Docker = class {
|
||||
listContainers = sinon.stub().resolves([
|
||||
{
|
||||
@@ -311,7 +326,7 @@ describe("Network Service", () => {
|
||||
expect(dockerResult.code).to.equal(networkService.NETWORK_ERROR);
|
||||
});
|
||||
|
||||
it("should throw an error if operations fail", async () => {
|
||||
it("should throw an error if operations fail", async function() {
|
||||
Docker = class {
|
||||
listContainers = sinon.stub().resolves([
|
||||
{
|
||||
@@ -329,7 +344,8 @@ describe("Network Service", () => {
|
||||
expect(error.message).to.equal("test error");
|
||||
}
|
||||
});
|
||||
it("should throw an error if no matching images found", async () => {
|
||||
|
||||
it("should throw an error if no matching images found", async function() {
|
||||
Docker = class {
|
||||
listContainers = sinon.stub().resolves([]);
|
||||
getContainer = sinon.stub().throws(new Error("test error"));
|
||||
@@ -344,8 +360,8 @@ describe("Network Service", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("getStatus", () => {
|
||||
beforeEach(() => {
|
||||
describe("getStatus", function() {
|
||||
beforeEach(function() {
|
||||
networkService.requestPing = sinon.stub();
|
||||
networkService.requestHttp = sinon.stub();
|
||||
networkService.requestPagespeed = sinon.stub();
|
||||
@@ -353,45 +369,51 @@ describe("Network Service", () => {
|
||||
networkService.requestDocker = sinon.stub();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
afterEach(function() {
|
||||
sinon.restore();
|
||||
});
|
||||
it("should call requestPing if type is ping", async () => {
|
||||
|
||||
it("should call requestPing if type is ping", async function() {
|
||||
await networkService.getStatus({ data: { type: "ping" } });
|
||||
expect(networkService.requestPing.calledOnce).to.be.true;
|
||||
expect(networkService.requestDocker.notCalled).to.be.true;
|
||||
expect(networkService.requestHttp.notCalled).to.be.true;
|
||||
expect(networkService.requestPagespeed.notCalled).to.be.true;
|
||||
});
|
||||
it("should call requestHttp if type is http", async () => {
|
||||
|
||||
it("should call requestHttp if type is http", async function() {
|
||||
await networkService.getStatus({ data: { type: "http" } });
|
||||
expect(networkService.requestPing.notCalled).to.be.true;
|
||||
expect(networkService.requestDocker.notCalled).to.be.true;
|
||||
expect(networkService.requestHttp.calledOnce).to.be.true;
|
||||
expect(networkService.requestPagespeed.notCalled).to.be.true;
|
||||
});
|
||||
it("should call requestPagespeed if type is pagespeed", async () => {
|
||||
|
||||
it("should call requestPagespeed if type is pagespeed", async function() {
|
||||
await networkService.getStatus({ data: { type: "pagespeed" } });
|
||||
expect(networkService.requestPing.notCalled).to.be.true;
|
||||
expect(networkService.requestDocker.notCalled).to.be.true;
|
||||
expect(networkService.requestHttp.notCalled).to.be.true;
|
||||
expect(networkService.requestPagespeed.calledOnce).to.be.true;
|
||||
});
|
||||
it("should call requestHardware if type is hardware", async () => {
|
||||
|
||||
it("should call requestHardware if type is hardware", async function() {
|
||||
await networkService.getStatus({ data: { type: "hardware" } });
|
||||
expect(networkService.requestHardware.calledOnce).to.be.true;
|
||||
expect(networkService.requestDocker.notCalled).to.be.true;
|
||||
expect(networkService.requestPing.notCalled).to.be.true;
|
||||
expect(networkService.requestPagespeed.notCalled).to.be.true;
|
||||
});
|
||||
it("should call requestDocker if type is Docker", async () => {
|
||||
|
||||
it("should call requestDocker if type is Docker", async function() {
|
||||
await networkService.getStatus({ data: { type: "docker" } });
|
||||
expect(networkService.requestDocker.calledOnce).to.be.true;
|
||||
expect(networkService.requestHardware.notCalled).to.be.true;
|
||||
expect(networkService.requestPing.notCalled).to.be.true;
|
||||
expect(networkService.requestPagespeed.notCalled).to.be.true;
|
||||
});
|
||||
it("should throw an error if an unknown type is provided", async () => {
|
||||
|
||||
it("should throw an error if an unknown type is provided", async function() {
|
||||
try {
|
||||
await networkService.getStatus({ data: { type: "unknown" } });
|
||||
} catch (error) {
|
||||
@@ -400,7 +422,8 @@ describe("Network Service", () => {
|
||||
expect(error.message).to.equal("Unsupported type: unknown");
|
||||
}
|
||||
});
|
||||
it("should throw an error if job type is undefined", async () => {
|
||||
|
||||
it("should throw an error if job type is undefined", async function() {
|
||||
try {
|
||||
await networkService.getStatus({ data: { type: undefined } });
|
||||
} catch (error) {
|
||||
@@ -409,7 +432,8 @@ describe("Network Service", () => {
|
||||
expect(error.message).to.equal("Unsupported type: unknown");
|
||||
}
|
||||
});
|
||||
it("should throw an error if job is empty", async () => {
|
||||
|
||||
it("should throw an error if job is empty", async function() {
|
||||
try {
|
||||
await networkService.getStatus({});
|
||||
} catch (error) {
|
||||
@@ -417,7 +441,8 @@ describe("Network Service", () => {
|
||||
expect(error.message).to.equal("Unsupported type: unknown");
|
||||
}
|
||||
});
|
||||
it("should throw an error if job is null", async () => {
|
||||
|
||||
it("should throw an error if job is null", async function() {
|
||||
try {
|
||||
await networkService.getStatus(null);
|
||||
} catch (error) {
|
||||
|
||||
Reference in new issue
Block a user