Skip to content

Commit

Permalink
chore: improve-cov
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomer Levy authored and Tomer Levy committed Dec 9, 2023
1 parent 7d4a96a commit 6a30ebf
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 4 deletions.
25 changes: 24 additions & 1 deletion tests/kafkaMessageBus.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { KafkaMessageBus } from "../src/kafkaMessageBus";
import InMemoryProvider from '../src/providers/inMemory';
import { KafkaConfig } from '../src/interfaces';
import { KafkaConfig, KafkaProducerMessage } from '../src/interfaces';
import { Provider, Providers} from "../src/types";
import invalidMiddleware from './middlewares/invalidMiddleware';

describe('KafkaMessageBus', () => {
let config1: KafkaConfig;
Expand Down Expand Up @@ -44,11 +45,33 @@ describe('KafkaMessageBus', () => {
expect(() => new KafkaMessageBus({ config: config1 })).toThrow('provider: unsupported is not supported');
});

it('should throw an error when listening to unsupported event', () => {
const bus = new KafkaMessageBus({ config: config2 });
expect(() => bus.on('unsupported' as any, jest.fn())).toThrow('Invalid event type unsupported');
});

it('should throw an error when listening with non functional callback', () => {
const bus = new KafkaMessageBus({ config: config2 });
expect(() => bus.on('log', {})).toThrow('callback argument must be of type function');
});

it('should create an instance of KafkaMessageBus with inMemory provider', () => {
const bus = new KafkaMessageBus({ config: config2 });
expect(bus).toBeInstanceOf(KafkaMessageBus);
});

it('should failed to send message due to invalid middleware', async () => {
const bus = new KafkaMessageBus({ config: config2, middlewares: [invalidMiddleware] });
const topic = 'undefinedTopic';
const message: KafkaProducerMessage = {
key: "testKey",
messageType: "testType",
value: { content: "testContent" }
};

await expect(bus.send({ topic, message })).rejects.toThrow('next() called multiple times');
});

it('should use InMemoryProvider for specific tests', () => {

Check warning on line 75 in tests/kafkaMessageBus.test.ts

View workflow job for this annotation

GitHub Actions / lint (20.x)

Test has no assertions
new InMemoryProvider({ config: config2 });
});
Expand Down
9 changes: 9 additions & 0 deletions tests/middlewares/invalidMiddleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { EachMessagePayload } from 'kafkajs';
import { Middleware } from '../../src';

const middleware: Middleware<EachMessagePayload> = async (_eachMessage, next) => {
await next();
await next()
};

export default middleware
2 changes: 1 addition & 1 deletion tests/middleware.ts → tests/middlewares/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { EachMessagePayload } from 'kafkajs';
import { Middleware } from '../src';
import { Middleware } from '../../src';

const middleware: Middleware<EachMessagePayload> = async (_eachMessage, next) => {
await next()
Expand Down
2 changes: 1 addition & 1 deletion tests/providers/inMemory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { KafkaMessageBus } from "../../src/kafkaMessageBus";
import { ConsumerDefinition, KafkaProducerMessage, KafkaConfig } from '../../src/interfaces'
import { Providers } from "../../src/types";
import { v4 as uuidv4 } from 'uuid';
import middleware from '../middleware';
import middleware from '../middlewares/middleware';

describe('KafkaMessageBus with InMemoryProvider', () => {
let kafkaMessageBus: KafkaMessageBus;
Expand Down
2 changes: 1 addition & 1 deletion tests/providers/kafka.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { KafkaMessageBus } from "../../src/kafkaMessageBus";
import { ConsumerDefinition, KafkaProducerMessage, KafkaConfig } from '../../src/interfaces';
import { Providers } from "../../src/types";
import { v4 as uuidv4 } from 'uuid';
import middleware from '../middleware';
import middleware from '../middlewares/middleware';

describe('KafkaMessageBus with KafkaProvider', () => {
let kafkaMessageBus: KafkaMessageBus;
Expand Down

0 comments on commit 6a30ebf

Please sign in to comment.