Skip to content

Commit

Permalink
test: tweak BE test setup
Browse files Browse the repository at this point in the history
  • Loading branch information
getlarge committed Dec 13, 2023
1 parent 48a5d7e commit 1249b94
Show file tree
Hide file tree
Showing 26 changed files with 193 additions and 161 deletions.
14 changes: 7 additions & 7 deletions apps/auth/jest-e2e.config.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
export default {
displayName: 'auth',

globals: {
'ts-jest': {
tsconfig: '<rootDir>/tsconfig.spec.json',
},
},
globals: {},
testEnvironment: 'node',
transform: {
'^.+\\.[t]s$': 'ts-jest',
'^.+\\.[t]s$': [
'ts-jest',
{
tsconfig: '<rootDir>/tsconfig.spec.json',
},
],
},
setupFilesAfterEnv: ['<rootDir>/jest.setup.js'],
moduleFileExtensions: ['ts', 'js', 'node'],
Expand Down
14 changes: 7 additions & 7 deletions apps/auth/jest.config.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
export default {
displayName: 'auth',

globals: {
'ts-jest': {
tsconfig: '<rootDir>/tsconfig.spec.json',
},
},
globals: {},
testEnvironment: 'node',
transform: {
'^.+\\.[tj]s$': 'ts-jest',
'^.+\\.[t]s$': [
'ts-jest',
{
tsconfig: '<rootDir>/tsconfig.spec.json',
},
],
},
moduleFileExtensions: ['ts', 'js', 'html'],
coverageDirectory: '../../coverage/apps/auth',
Expand Down
14 changes: 7 additions & 7 deletions apps/expiration/jest.config.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
export default {
displayName: 'expiration',

globals: {
'ts-jest': {
tsconfig: '<rootDir>/tsconfig.spec.json',
},
},
globals: {},
testEnvironment: 'node',
transform: {
'^.+\\.[tj]s$': 'ts-jest',
'^.+\\.[t]s$': [
'ts-jest',
{
tsconfig: '<rootDir>/tsconfig.spec.json',
},
],
},
moduleFileExtensions: ['ts', 'js', 'html'],
coverageDirectory: '../../coverage/apps/expiration',
Expand Down
13 changes: 6 additions & 7 deletions apps/orders/jest-e2e.config.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
export default {
displayName: 'orders',

globals: {
'ts-jest': {
tsconfig: '<rootDir>/tsconfig.spec.json',
},
},
testEnvironment: 'node',
transform: {
'^.+\\.[t]s$': 'ts-jest',
'^.+\\.[t]s$': [
'ts-jest',
{
tsconfig: '<rootDir>/tsconfig.spec.json',
},
],
},
setupFilesAfterEnv: ['<rootDir>/jest.setup.js'],
moduleFileExtensions: ['ts', 'js', 'node'],
Expand Down
14 changes: 7 additions & 7 deletions apps/orders/jest.config.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
export default {
displayName: 'orders',

globals: {
'ts-jest': {
tsconfig: '<rootDir>/tsconfig.spec.json',
},
},
globals: {},
testEnvironment: 'node',
transform: {
'^.+\\.[tj]s$': 'ts-jest',
'^.+\\.[t]s$': [
'ts-jest',
{
tsconfig: '<rootDir>/tsconfig.spec.json',
},
],
},
moduleFileExtensions: ['ts', 'js', 'html'],
coverageDirectory: '../../coverage/apps/orders',
Expand Down
7 changes: 6 additions & 1 deletion apps/orders/src/app/orders/orders-ms.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
MockModel,
MockPublisher,
} from '@ticketing/microservices/shared/testing';
import { Channel } from 'amqp-connection-manager';
import { Types } from 'mongoose';

import { OrdersService } from './orders.service';
Expand Down Expand Up @@ -58,12 +59,16 @@ describe('OrdersMSController', () => {
const ordersService = app.get(OrdersService);
ordersService.expireById = jest.fn().mockRejectedValueOnce(expectedError);
context.getChannelRef().ack = jest.fn();
const channel = context.getChannelRef() as Channel;
channel.ack = jest.fn();
channel.nack = jest.fn();
//
await expect(
ordersController.onExpiration(order, context),
).rejects.toThrowError(expectedError);
expect(ordersService.expireById).toBeCalledWith(order.id);
expect(context.getChannelRef().ack).not.toBeCalled();
expect(channel.ack).not.toBeCalled();
expect(channel.nack).toBeCalled();
});
});
});
23 changes: 15 additions & 8 deletions apps/orders/src/app/tickets/tickets-ms.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
createRmqContext,
MockModel,
} from '@ticketing/microservices/shared/testing';
import { Channel } from 'amqp-connection-manager';

import { mockTicketEvent } from '../../../test/models/ticket.mock';
import { TicketsService } from './tickets.service';
Expand All @@ -26,7 +27,7 @@ describe('TicketsMSController', () => {
});

describe('onCreated()', () => {
it('should call "TicketsService.create" and in case of success ack NATS message', async () => {
it('should call "TicketsService.create" and in case of success ack RMQ message', async () => {
// ticket coming from tickets-service
const ticket = mockTicketEvent();
const context = createRmqContext();
Expand All @@ -41,7 +42,7 @@ describe('TicketsMSController', () => {
expect(context.getChannelRef().ack).toBeCalled();
});

it('should call "TicketsService.create" and in case of error NOT ack NATS message', async () => {
it('should call "TicketsService.create" and in case of error NOT ack RMQ message', async () => {
// ticket coming from tickets-service
const ticket = mockTicketEvent();
const context = createRmqContext();
Expand All @@ -50,18 +51,21 @@ describe('TicketsMSController', () => {
app.get<TicketsMSController>(TicketsMSController);
const ticketsService = app.get<TicketsService>(TicketsService);
ticketsService.create = jest.fn().mockRejectedValueOnce(expectedError);
context.getChannelRef().ack = jest.fn();
const channel = context.getChannelRef() as Channel;
channel.ack = jest.fn();
channel.nack = jest.fn();
//
await expect(
ticketsController.onCreated(ticket, context),
).rejects.toThrowError(expectedError);
expect(ticketsService.create).toBeCalledWith(ticket);
expect(context.getChannelRef().ack).not.toBeCalled();
expect(channel.ack).not.toBeCalled();
expect(channel.nack).toBeCalled();
});
});

describe('onUpdated()', () => {
it('should call "TicketsService.updatedById" and in case of success, ack NATS message', async () => {
it('should call "TicketsService.updatedById" and in case of success, ack RMQ message', async () => {
// ticket coming from tickets-service
const ticket = mockTicketEvent();
const context = createRmqContext();
Expand All @@ -76,7 +80,7 @@ describe('TicketsMSController', () => {
expect(context.getChannelRef().ack).toBeCalled();
});

it('should call "TicketsService.updatedById" and in case of error, NOT ack NATS message', async () => {
it('should call "TicketsService.updatedById" and in case of error, NOT ack RMQ message', async () => {
// ticket coming from tickets-service
const ticket = mockTicketEvent();
const context = createRmqContext();
Expand All @@ -87,13 +91,16 @@ describe('TicketsMSController', () => {
ticketsService.updateById = jest
.fn()
.mockRejectedValueOnce(expectedError);
context.getChannelRef().ack = jest.fn();
const channel = context.getChannelRef() as Channel;
channel.ack = jest.fn();
channel.nack = jest.fn();
//
await expect(
ticketsController.onUpdated(ticket, context),
).rejects.toThrowError(expectedError);
expect(ticketsService.updateById).toBeCalledWith(ticket.id, ticket);
expect(context.getChannelRef().ack).not.toBeCalled();
expect(channel.ack).not.toBeCalled();
expect(channel.nack).toBeCalled();
});
});
});
14 changes: 7 additions & 7 deletions apps/payments/jest-e2e.config.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
export default {
displayName: 'payments',

globals: {
'ts-jest': {
tsconfig: '<rootDir>/tsconfig.spec.json',
},
},
globals: {},
testEnvironment: 'node',
transform: {
'^.+\\.[t]s$': 'ts-jest',
'^.+\\.[t]s$': [
'ts-jest',
{
tsconfig: '<rootDir>/tsconfig.spec.json',
},
],
},
setupFilesAfterEnv: ['<rootDir>/jest.setup.js'],
moduleFileExtensions: ['ts', 'js', 'node'],
Expand Down
14 changes: 7 additions & 7 deletions apps/payments/jest.config.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
export default {
displayName: 'payments',

globals: {
'ts-jest': {
tsconfig: '<rootDir>/tsconfig.spec.json',
},
},
globals: {},
testEnvironment: 'node',
transform: {
'^.+\\.[tj]s$': 'ts-jest',
'^.+\\.[t]s$': [
'ts-jest',
{
tsconfig: '<rootDir>/tsconfig.spec.json',
},
],
},
moduleFileExtensions: ['ts', 'js', 'html'],
coverageDirectory: '../../coverage/apps/payments',
Expand Down
2 changes: 0 additions & 2 deletions apps/payments/test/payments.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,6 @@ describe('PaymentsController (e2e)', () => {
paymentModel = app.get<Model<PaymentDocument>>(
getModelToken(PaymentSchema.name),
);
// eslint-disable-next-line @typescript-eslint/no-unused-vars
// natsClient = app.get(Publisher);
await app.init();
});

Expand Down
13 changes: 7 additions & 6 deletions apps/tickets/jest-e2e.config.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
export default {
displayName: 'tickets',

globals: {
'ts-jest': {
tsconfig: '<rootDir>/tsconfig.spec.json',
},
},
globals: {},
testEnvironment: 'node',
transform: {
'^.+\\.[t]s$': 'ts-jest',
'^.+\\.[t]s$': [
'ts-jest',
{
tsconfig: '<rootDir>/tsconfig.spec.json',
},
],
},
setupFilesAfterEnv: ['<rootDir>/jest.setup.js'],
moduleFileExtensions: ['ts', 'js', 'node'],
Expand Down
14 changes: 7 additions & 7 deletions apps/tickets/jest.config.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
export default {
displayName: 'tickets',

globals: {
'ts-jest': {
tsconfig: '<rootDir>/tsconfig.spec.json',
},
},
globals: {},
testEnvironment: 'node',
transform: {
'^.+\\.[tj]s$': 'ts-jest',
'^.+\\.[t]s$': [
'ts-jest',
{
tsconfig: '<rootDir>/tsconfig.spec.json',
},
],
},
moduleFileExtensions: ['ts', 'js', 'html'],
coverageDirectory: '../../coverage/apps/tickets',
Expand Down
23 changes: 15 additions & 8 deletions apps/tickets/src/app/orders/orders-ms.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
MockModel,
MockPublisher,
} from '@ticketing/microservices/shared/testing';
import { Channel } from 'amqp-connection-manager';

import { mockOrderEvent } from '../../../test/models/order.mock';
import { TicketsService } from '../tickets/tickets.service';
Expand All @@ -30,7 +31,7 @@ describe('OrdersMSController', () => {
});

describe('onCreated()', () => {
it('should call "TicketsService.createOrder" and in case of success ack NATS message', async () => {
it('should call "TicketsService.createOrder" and in case of success ack RMQ message', async () => {
// ticket coming from tickets-service
const order = mockOrderEvent();
const context = createRmqContext();
Expand All @@ -44,7 +45,7 @@ describe('OrdersMSController', () => {
expect(context.getChannelRef().ack).toBeCalled();
});

it('should call "TicketsService.createOrder" and in case of error NOT ack NATS message', async () => {
it('should call "TicketsService.createOrder" and in case of error NOT ack RMQ message', async () => {
// ticket coming from tickets-service
const order = mockOrderEvent();
const context = createRmqContext();
Expand All @@ -54,18 +55,21 @@ describe('OrdersMSController', () => {
ticketsService.createOrder = jest
.fn()
.mockRejectedValueOnce(expectedError);
context.getChannelRef().ack = jest.fn();
const channel = context.getChannelRef() as Channel;
channel.ack = jest.fn();
channel.nack = jest.fn();
//
await expect(
ordersMSController.onCreated(order, context),
).rejects.toThrowError(expectedError);
expect(ticketsService.createOrder).toBeCalledWith(order);
expect(context.getChannelRef().ack).not.toBeCalled();
expect(channel.ack).not.toBeCalled();
expect(channel.nack).toBeCalled();
});
});

describe('onCancelled()', () => {
it('should call "TicketsService.cancelOrder" and in case of success, ack NATS message', async () => {
it('should call "TicketsService.cancelOrder" and in case of success, ack RMQ message', async () => {
// ticket coming from tickets-service
const order = mockOrderEvent();
const context = createRmqContext();
Expand All @@ -79,7 +83,7 @@ describe('OrdersMSController', () => {
expect(context.getChannelRef().ack).toBeCalled();
});

it('should call "TicketsService.cancelOrder" and in case of error, NOT ack NATS message', async () => {
it('should call "TicketsService.cancelOrder" and in case of error, NOT ack RMQ message', async () => {
// ticket coming from tickets-service
const order = mockOrderEvent();
const context = createRmqContext();
Expand All @@ -89,13 +93,16 @@ describe('OrdersMSController', () => {
ticketsService.cancelOrder = jest
.fn()
.mockRejectedValueOnce(expectedError);
context.getChannelRef().ack = jest.fn();
const channel = context.getChannelRef() as Channel;
channel.ack = jest.fn();
channel.nack = jest.fn();
//
await expect(
ordersMSController.onCancelled(order, context),
).rejects.toThrowError(expectedError);
expect(ticketsService.cancelOrder).toBeCalledWith(order);
expect(context.getChannelRef().ack).not.toBeCalled();
expect(channel.ack).not.toBeCalled();
expect(channel.nack).toBeCalled();
});
});
});
Loading

0 comments on commit 1249b94

Please sign in to comment.