Skip to content

Commit

Permalink
style: lint unit tests
Browse files Browse the repository at this point in the history
[ci skip]
  • Loading branch information
Bugs5382 committed Dec 27, 2023
1 parent 97bf62f commit c43ab80
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 85 deletions.
3 changes: 1 addition & 2 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
jest.config.ts
release.config.js
/__tests__/**/*
release.config.cjs
18 changes: 9 additions & 9 deletions __tests__/__utils__/utils.ts
Original file line number Diff line number Diff line change
@@ -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<any> {
return await new Promise(resolve => setTimeout(resolve, ms))
}

export interface Deferred<T=any> {
Expand All @@ -11,19 +11,19 @@ export interface Deferred<T=any> {
promise: Promise<T>
}

export function createDeferred<T=any>(noUncaught?: boolean): Deferred<T> {
let dfd: any = {}
export function createDeferred<T=any> (noUncaught?: boolean): Deferred<T> {
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<T=any>(emitter: EventEmitter, name: string|symbol): Promise<T> {
return new Promise<T>((resolve) => { emitter.once(name, resolve) })
}
export async function expectEvent<T=any> (emitter: EventEmitter, name: string | symbol): Promise<T> {
return await new Promise<T>((resolve) => { emitter.once(name, resolve) })
}
65 changes: 22 additions & 43 deletions __tests__/app.test.ts
Original file line number Diff line number Diff line change
@@ -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 () => {
Expand All @@ -23,11 +19,9 @@ describe('fastify-rabbitmq sample app tests', () => {
await app.listen()

await app.ready()

})

afterEach(async () => {

await pub.close()

await sub.close()
Expand All @@ -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<void>()
const dfd = createDeferred<void>() // 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)
Expand All @@ -63,48 +57,40 @@ 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)

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, {
Expand All @@ -115,11 +101,9 @@ describe('fastify-rabbitmq sample app tests', () => {
await app.listen()

await app.ready()

})

afterEach(async () => {

await pub.close()

await sub.close()
Expand All @@ -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<void>()
const dfd = createDeferred<void>() // 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)
Expand All @@ -155,36 +139,31 @@ 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)

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')

})

})

})
14 changes: 6 additions & 8 deletions __tests__/rabbitmq.test.ts
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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.'))
}
Expand All @@ -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.'))
Expand All @@ -44,15 +44,14 @@ 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: []
})
} catch (error) {
expect(error).toEqual(new errors.FASTIFY_RABBIT_MQ_ERR_INVALID_OPTS('urls must contain one or more item in the array.'))
}
})

})

describe('sanity checks', () => {
Expand Down Expand Up @@ -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', () => {
Expand Down
23 changes: 0 additions & 23 deletions bin/make-docs.sh

This file was deleted.

0 comments on commit c43ab80

Please sign in to comment.