Skip to content

Commit

Permalink
address all eslint typescript warnings/errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jbielick committed Aug 11, 2020
1 parent e161277 commit 4d03c51
Show file tree
Hide file tree
Showing 35 changed files with 555 additions and 433 deletions.
3 changes: 2 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
*.test.js
*.test.ts
lib
14 changes: 13 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
{
"extends": "airbnb-base",
"parser": "@typescript-eslint/parser",
"plugins": [
"@typescript-eslint"
],
"extends": [
"plugin:@typescript-eslint/recommended",
"prettier/@typescript-eslint",
"plugin:prettier/recommended"
],
"parserOptions": {
"ecmaVersion": 2020,
"sourceType": "module"
},
"env": {
"node": true,
"es6": true
Expand Down
2 changes: 1 addition & 1 deletion .npmignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
test
__tests__
*.test.js
docs
coverage
Expand Down
1 change: 1 addition & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = {};
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ before_script:
done
script:
- npm run lint
- npm run build
- npm test
after_success:
- './node_modules/.bin/nyc report --reporter=text-lcov | ./node_modules/.bin/coveralls'
2 changes: 0 additions & 2 deletions bin/faktory-work
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#!/usr/bin/env node

const faktory = require("../lib/faktory");
console.log(faktory);
process.exit(0);
const program = require("../lib/cli");

faktory.work(program).catch((error) => {
Expand Down
24 changes: 17 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
"watch": "tsc -w",
"build": "tsc",
"clean": "rm -rf lib/*",
"test": "nyc --cache ava src/__tests__/*",
"test": "nyc --cache ava src/__tests__/**/*",
"test:unit": "nyc --cache ava src/__tests__/unit/*",
"test:integration": "nyc --cache ava src/__tests__/integration/*",
"test:watch": "npm run test -- -w",
"coverage": "nyc report --reporter=html",
"lint": "eslint src",
"lint": "eslint . --ext .ts",
"htmldocs": "jsdoc . lib -d docs --readme README.md --pedantic",
"docs": "jsdoc2md --files lib/*.js --template docs/template.hbs > docs/api.md",
"preversion": "npm run lint && npm run test && npm run docs",
"preversion": "npm run lint && npm run build && npm run test && npm run docs",
"version": "git add docs/ package.json",
"postversion": "git push && git push --tags"
},
Expand Down Expand Up @@ -49,27 +51,35 @@
"engines": {
"node": ">=7"
},
"types": "lib/index.d.ts",
"devDependencies": {
"@ava/typescript": "^1.1.1",
"@types/debug": "^4.1.5",
"@types/generic-pool": "^3.1.9",
"@types/koa-compose": "^3.2.5",
"@types/node": "^14.0.11",
"@types/uuid": "^8.0.0",
"@typescript-eslint/eslint-plugin": "^3.1.0",
"@typescript-eslint/parser": "^3.1.0",
"ava": "^3.8.2",
"coveralls": "3.1.0",
"eslint": "^4.10",
"eslint-config-airbnb-base": "13.2.0",
"eslint-plugin-import": "2.18.2",
"eslint": "^7.2.0",
"eslint-config-prettier": "^6.11.0",
"eslint-plugin-import": "^2.21.1",
"eslint-plugin-prettier": "^3.1.3",
"get-port": "4.2.0",
"jsdoc": "3.6.3",
"jsdoc-to-markdown": "^3.1.0-1",
"nyc": "13.3.0",
"prettier": "^2.0.5",
"sinon": "9.0.2",
"typescript": "^3.9.5"
},
"dependencies": {
"commander": "2.20.0",
"debug": "^4.1.0",
"generic-pool": "3.7.1",
"koa-compose": "^4.0.0",
"koa-compose": "^4.1.0",
"redis-parser": "^3.0.0",
"uuid": "3.3.3"
}
Expand Down
15 changes: 15 additions & 0 deletions src/@types/redis-parser/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
declare module "redis-parser" {
type Config = {
returnReply: (response: string) => void;
returnError: (error: Error) => void;
};

class RedisParser {
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
constructor(config: Config);
execute(buffer: Buffer): (err: Error | null, response: string) => void;
}

// export = RedisParser;
export default RedisParser;
}
39 changes: 23 additions & 16 deletions src/__tests__/_helper.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import { TestInterface } from 'ava';
import { Socket, createServer, Server } from 'net';
const uuid = require('uuid/v4');
import getPort from 'get-port';
import Client from '../client';
import { TestInterface } from "ava";
import { Socket, createServer, Server } from "net";
import { v4 as uuid } from "uuid";
import getPort from "get-port";
import Client from "../client";
import { JobPayload, PartialJobPayload } from "../job";
import { Command } from "../connection";

export type ServerControl = {
socket: Socket;
command?: string;
data?: string;
};

export const mockServer = () => {
export const mockServer = (): Server => {
const server = createServer();

server.on("connection", (socket) => {
Expand All @@ -38,7 +37,11 @@ export const mockServer = () => {
return server;
};

export const mocked = async (fn: (server: Server, port: number) => any | void) => {
type ServerUser = {
(server: Server, port: number): Promise<unknown>;
};

export const mocked = async (fn: ServerUser): ReturnType<ServerUser> => {
const server = mockServer();
const port = await getPort();
server.listen(port, "127.0.0.1");
Expand Down Expand Up @@ -81,25 +84,29 @@ mocked.info = () => ({ socket }: ServerControl) => {
socket.write(`$${json.length}\r\n${json}\r\n`);
};

export const sleep = (ms: number, value: any = true) => {
export const sleep = (ms: number, value?: unknown): Promise<unknown> => {
return new Promise((resolve) => setTimeout(() => resolve(value), ms));
};

export const randQueue = (label: string = "test") => {
export const randQueue = (label = "test"): string => {
return `${label}-${uuid().slice(0, 6)}`;
};

export const createJob = (...args: any[]): PartialJobPayload => {
export const createJob = (...args: unknown[]): PartialJobPayload => {
return {
jobtype: "testJob",
queue: randQueue(),
args,
};
};

export const push = async (
{ args, queue, jobtype }: { args?: any[], queue?: string, jobtype?: string } = {}
) => {
export const push = async ({
args,
queue,
jobtype,
}: { args?: unknown[]; queue?: string; jobtype?: string } = {}): Promise<
JobPayload
> => {
const client = new Client();

const job = client.job(jobtype || "test");
Expand All @@ -113,9 +120,9 @@ export const push = async (
return job;
};

export const flush = () => new Client().flush();
export const flush = (): Promise<string> => new Client().flush();

export function registerCleaner(test: TestInterface) {
export function registerCleaner(test: TestInterface): void {
test.beforeEach(async () => {
await flush();
});
Expand Down
20 changes: 11 additions & 9 deletions src/__tests__/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,22 @@ test("#buildHello: client builds a passwordless ahoy", (t) => {
});

test("#buildHello: client builds a salty ahoy", (t) => {
const client = new Client();
const client = new Client({
password: "abcde123",
});

const hello = client.buildHello({ i: 3, s: "123", v: 3 });

t.is(
hello.pwdhash,
"3180b4071170db0ae9f666167ed379f53468463f152e3c3cfb57d1de45fd01d6"
"ef646abadf4ffba660d9bbb8de8e45576970de917b4c9da8cad96b49e64636d9"
);
});

test("#buildHello: wid is present in HELLO", (t) => {
const client = new Client({ wid: "workerid" });

const hello = client.buildHello({ v: 2, s: 'abc', i: 3 });
const hello = client.buildHello({ v: 2, s: "abc", i: 3 });

t.is(hello.wid, client.wid, "wid in ahoy does not match");
});
Expand Down Expand Up @@ -144,7 +146,7 @@ test("#beat: sends a heartbeat", async (t) => {
});

test("#beat: returns a signal from the server", async (t) => {
return mocked(async (server, port) => {
await mocked(async (server, port) => {
server.on("BEAT", mocked.beat("quiet"));
const client = new Client({ port });

Expand All @@ -170,7 +172,7 @@ test("#connect: rejects if handshake is not successful", async (t) => {

test("#connect: connects explicitly", async (t) => {
t.plan(2);
return mocked(async (server, port) => {
await mocked(async (server, port) => {
server
.on("HELLO", () => {
t.is(1, 1);
Expand Down Expand Up @@ -202,7 +204,7 @@ test("#ack: ACKs a job", async (t) => {
});

test("#fetch: returns null when queue is empty", async (t) => {
return mocked(async (server, port) => {
await mocked(async (server, port) => {
server.on("FETCH", ({ socket }) => {
// null bulkstring
socket.write("$-1\r\n");
Expand All @@ -215,7 +217,7 @@ test("#fetch: returns null when queue is empty", async (t) => {

test("#push: defaults job payload values according to spec", async (t) => {
let serverJob: JobPayload;
return mocked(async (server, port) => {
await mocked(async (server, port) => {
server.on("PUSH", ({ data, socket }) => {
serverJob = data;
socket.write("+OK\r\n");
Expand Down Expand Up @@ -282,7 +284,7 @@ test("#job: provides the args to the job", (t) => {
});

test("#job: push sends job specification to server", async (t) => {
return mocked(async (server, port) => {
await mocked(async (server, port) => {
server.on("PUSH", ({ data, socket }) => {
socket.write("+OK\r\n");
const { jobtype, args, custom, retry } = data;
Expand All @@ -302,7 +304,7 @@ test("#job: push sends job specification to server", async (t) => {
});

test("#job: push resolves with the jid", async (t) => {
return mocked(async (server, port) => {
await mocked(async (server, port) => {
server.on("PUSH", ({ data, socket }) => {
socket.write("+OK\r\n");
});
Expand Down
Loading

0 comments on commit 4d03c51

Please sign in to comment.