From c43ab80e99a207da08222eddd7c903f704db06f5 Mon Sep 17 00:00:00 2001 From: Bugs5382 Date: Wed, 27 Dec 2023 11:32:51 -0500 Subject: [PATCH] style: lint unit tests [ci skip] --- .eslintignore | 3 +- __tests__/__utils__/utils.ts | 18 +++++----- __tests__/app.test.ts | 65 ++++++++++++------------------------ __tests__/rabbitmq.test.ts | 14 ++++---- bin/make-docs.sh | 23 ------------- 5 files changed, 38 insertions(+), 85 deletions(-) delete mode 100644 bin/make-docs.sh diff --git a/.eslintignore b/.eslintignore index 9ef7374..b4192db 100644 --- a/.eslintignore +++ b/.eslintignore @@ -1,3 +1,2 @@ jest.config.ts -release.config.js -/__tests__/**/* \ No newline at end of file +release.config.cjs \ No newline at end of file diff --git a/__tests__/__utils__/utils.ts b/__tests__/__utils__/utils.ts index 4df0b61..8d0caf0 100644 --- a/__tests__/__utils__/utils.ts +++ b/__tests__/__utils__/utils.ts @@ -1,8 +1,8 @@ /* istanbul ignore next */ -import EventEmitter from "node:events"; +import EventEmitter from 'node:events' -export function sleep(ms: number) { - return new Promise(resolve => setTimeout(resolve, ms)) +export async function sleep (ms: number): Promise { + return await new Promise(resolve => setTimeout(resolve, ms)) } export interface Deferred { @@ -11,19 +11,19 @@ export interface Deferred { promise: Promise } -export function createDeferred(noUncaught?: boolean): Deferred { - let dfd: any = {} +export function createDeferred (noUncaught?: boolean): Deferred { + const dfd: any = {} dfd.promise = new Promise((resolve, reject) => { dfd.resolve = resolve dfd.reject = reject }) /* istanbul ignore next */ - if (noUncaught) { + if (noUncaught) { // eslint-disable-line dfd.promise.catch(() => {}) } return dfd } -export function expectEvent(emitter: EventEmitter, name: string|symbol): Promise { - return new Promise((resolve) => { emitter.once(name, resolve) }) -} \ No newline at end of file +export async function expectEvent (emitter: EventEmitter, name: string | symbol): Promise { + return await new Promise((resolve) => { emitter.once(name, resolve) }) +} diff --git a/__tests__/app.test.ts b/__tests__/app.test.ts index d0543f3..5f8c188 100644 --- a/__tests__/app.test.ts +++ b/__tests__/app.test.ts @@ -1,16 +1,12 @@ -import fastify, {FastifyInstance} from 'fastify' -import {Consumer, Publisher, RPCClient} from "rabbitmq-client"; -import fastifyRabbit from "../src"; -import {createDeferred, expectEvent, sleep} from "./__utils__/utils"; +import fastify, { FastifyInstance } from 'fastify' +import { Consumer, Publisher, RPCClient } from 'rabbitmq-client' +import fastifyRabbit from '../src' +import { createDeferred, expectEvent, sleep } from './__utils__/utils' describe('fastify-rabbitmq sample app tests', () => { - describe('no namespace', () => { - let app: FastifyInstance - // @ts-ignore let sub: Consumer - // @ts-ignore let pub: Publisher | RPCClient beforeEach(async () => { @@ -23,11 +19,9 @@ describe('fastify-rabbitmq sample app tests', () => { await app.listen() await app.ready() - }) afterEach(async () => { - await pub.close() await sub.close() @@ -40,18 +34,18 @@ describe('fastify-rabbitmq sample app tests', () => { test('create/get sender and receiver/listener (foo)', async () => { const LISTEN_QUEUE_NAME = 'foo' - const dfd = createDeferred() + const dfd = createDeferred() // eslint-disable-line await app.rabbitmq.queueDelete(LISTEN_QUEUE_NAME) sub = app.rabbitmq.createConsumer({ queue: LISTEN_QUEUE_NAME, - queueOptions: {durable: true}, - qos: {prefetchCount: 2} + queueOptions: { durable: true }, + qos: { prefetchCount: 2 } }, async (msg: any) => { - expect(msg.body.id).toBe(1); - expect(msg.body.name).toBe('Alan Turing'); - dfd.resolve(); + expect(msg.body.id).toBe(1) + expect(msg.body.name).toBe('Alan Turing') + dfd.resolve() }) await sleep(1) @@ -63,14 +57,12 @@ describe('fastify-rabbitmq sample app tests', () => { maxAttempts: 1 }) - await pub.send(LISTEN_QUEUE_NAME, {id: 1, name: 'Alan Turing'}) + await pub.send(LISTEN_QUEUE_NAME, { id: 1, name: 'Alan Turing' }) await dfd.promise - }) test('rpc', async () => { - const LISTEN_RPC_NAME = 'fooRPC' await app.rabbitmq.queueDelete(LISTEN_RPC_NAME) @@ -78,33 +70,27 @@ describe('fastify-rabbitmq sample app tests', () => { sub = app.rabbitmq.createConsumer({ queue: LISTEN_RPC_NAME }, async (_req: any, reply: any) => { - await reply('pong'); + await reply('pong') }) await sleep(1) await expectEvent(sub, 'ready') - pub = app.rabbitmq.createRPCClient({confirm: true}) + pub = app.rabbitmq.createRPCClient({ confirm: true }) const res = await pub.send(LISTEN_RPC_NAME, 'ping') expect(res.body).toBe('pong') - }) - }) describe('namespace', () => { - let app: FastifyInstance - // @ts-ignore let sub: Consumer - // @ts-ignore let pub: Publisher | RPCClient beforeEach(async () => { - app = fastify() await app.register(fastifyRabbit, { @@ -115,11 +101,9 @@ describe('fastify-rabbitmq sample app tests', () => { await app.listen() await app.ready() - }) afterEach(async () => { - await pub.close() await sub.close() @@ -132,18 +116,18 @@ describe('fastify-rabbitmq sample app tests', () => { test('create/get sender and receiver/listener (bar)', async () => { const LISTEN_QUEUE_NAME = 'bar' - const dfd = createDeferred() + const dfd = createDeferred() // eslint-disable-line await app.rabbitmq.unittest.queueDelete(LISTEN_QUEUE_NAME) sub = app.rabbitmq.unittest.createConsumer({ queue: LISTEN_QUEUE_NAME, - queueOptions: {durable: true}, - qos: {prefetchCount: 2} + queueOptions: { durable: true }, + qos: { prefetchCount: 2 } }, async (msg: any) => { - expect(msg.body.id).toBe(1); - expect(msg.body.name).toBe('Alan Turing'); - dfd.resolve(); + expect(msg.body.id).toBe(1) + expect(msg.body.name).toBe('Alan Turing') + dfd.resolve() }) await sleep(1) @@ -155,14 +139,12 @@ describe('fastify-rabbitmq sample app tests', () => { maxAttempts: 1 }) - await pub.send(LISTEN_QUEUE_NAME, {id: 1, name: 'Alan Turing'}) + await pub.send(LISTEN_QUEUE_NAME, { id: 1, name: 'Alan Turing' }) await dfd.promise - }) test('rpc', async () => { - const LISTEN_RPC_NAME = 'fooRPC' await app.rabbitmq.unittest.queueDelete(LISTEN_RPC_NAME) @@ -170,21 +152,18 @@ describe('fastify-rabbitmq sample app tests', () => { sub = app.rabbitmq.unittest.createConsumer({ queue: LISTEN_RPC_NAME }, async (_req: any, reply: any) => { - await reply('pong'); + await reply('pong') }) await sleep(1) await expectEvent(sub, 'ready') - pub = app.rabbitmq.unittest.createRPCClient({confirm: true}) + pub = app.rabbitmq.unittest.createRPCClient({ confirm: true }) const res = await pub.send(LISTEN_RPC_NAME, 'ping') expect(res.body).toBe('pong') - }) - }) - }) diff --git a/__tests__/rabbitmq.test.ts b/__tests__/rabbitmq.test.ts index eb37544..0c9c859 100644 --- a/__tests__/rabbitmq.test.ts +++ b/__tests__/rabbitmq.test.ts @@ -1,6 +1,6 @@ -import fastify, {FastifyInstance} from 'fastify' -import fastifyRabbit from "../src"; -import {errors} from '../src/errors' +import fastify, { FastifyInstance } from 'fastify' +import fastifyRabbit from '../src' +import { errors } from '../src/errors' let app: FastifyInstance @@ -25,7 +25,7 @@ describe('plugin fastify-rabbitmq tests', () => { test('register - error out - connection not defined', async () => { try { // @ts-expect-error - await app.register(fastifyRabbit, { connection: undefined}) + await app.register(fastifyRabbit, { connection: undefined }) } catch (error) { expect(error).toEqual(new errors.FASTIFY_RABBIT_MQ_ERR_INVALID_OPTS('connection or findServers must be defined.')) } @@ -35,7 +35,7 @@ describe('plugin fastify-rabbitmq tests', () => { try { // @ts-expect-error await app.register(fastifyRabbit, { - connection: 1, + connection: 1 }) } catch (error) { expect(error).toEqual(new errors.FASTIFY_RABBIT_MQ_ERR_INVALID_OPTS('urls must be defined.')) @@ -44,7 +44,7 @@ describe('plugin fastify-rabbitmq tests', () => { test('register - error out - urls less than 0', async () => { try { - // @ts-ignore + // @ts-expect-error await app.register(fastifyRabbit, { connection: [] }) @@ -52,7 +52,6 @@ describe('plugin fastify-rabbitmq tests', () => { expect(error).toEqual(new errors.FASTIFY_RABBIT_MQ_ERR_INVALID_OPTS('urls must contain one or more item in the array.')) } }) - }) describe('sanity checks', () => { @@ -85,7 +84,6 @@ describe('plugin fastify-rabbitmq tests', () => { expect(err).toEqual(new errors.FASTIFY_RABBIT_MQ_ERR_SETUP_ERRORS('Already registered with namespace: error')) } }) - }) describe('common action tests', () => { diff --git a/bin/make-docs.sh b/bin/make-docs.sh deleted file mode 100644 index c5dee0f..0000000 --- a/bin/make-docs.sh +++ /dev/null @@ -1,23 +0,0 @@ -#!/usr/bin/env bash -# Generate HTML documentation and commit to the gh-pages branch -# View with: python -m http.server -d docs 8000 -# -## Get current docs: -# git worktree add docs/ gh-pages -# -## Release Steps -# update package.json with version=$ver -# npm publish --dry-run -# git tag v$ver -# ./make-docs.sh -# git push origin master gh-pages v$ver -# npm publish -# -set -e - -# typedoc.json -node -r ts-node/register/transpile-only node_modules/.bin/typedoc - -#lver=$(git describe --long --tags --dirty) -#read -p "Commit to gh-pages as $lver? Press key to continue.. " -n1 -s -# cd docs && git commit --all --message="$lver" \ No newline at end of file