Skip to content

Commit

Permalink
added tests for returned errors
Browse files Browse the repository at this point in the history
  • Loading branch information
TheSlimvReal committed Dec 12, 2023
1 parent 1de7810 commit 17aa3ee
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 25 deletions.
86 changes: 63 additions & 23 deletions src/app.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
import { AppController } from './app.controller';
import { Test, TestingModule } from '@nestjs/testing';
import { firstValueFrom, of, throwError } from 'rxjs';
import { of, Subject, Subscription, throwError } from 'rxjs';
import { HttpService } from '@nestjs/axios';
import { BadRequestException, UnauthorizedException } from '@nestjs/common';
import { DeploymentInfo } from './deployment-info.dto';
import { ConfigModule } from '@nestjs/config';
import * as fs from 'fs';

let onSubscription: Subscription;
const lines = new Subject<string>();
jest.mock('tail', () => {
// Mock Tail constructor
return {
Tail: jest.fn().mockReturnValue({
on: (event, callback) => (onSubscription = lines.subscribe(callback)),
unwatch: () => onSubscription.unsubscribe(),
}),
};
});

describe('AppController', () => {
let controller: AppController;
let mockHttp: { post: jest.Mock };
let mockWs: { write: jest.Mock; close: jest.Mock };

const deploymentData: DeploymentInfo = {
name: 'test-name',
locale: 'de',
Expand All @@ -23,6 +37,8 @@ describe('AppController', () => {
};

beforeEach(async () => {
mockWs = { write: jest.fn(), close: jest.fn() };
jest.spyOn(fs, 'createWriteStream').mockReturnValue(mockWs as any);
mockHttp = {
post: jest.fn().mockReturnValue(of({ data: undefined })),
};
Expand Down Expand Up @@ -63,33 +79,57 @@ describe('AppController', () => {
});
});

it('should write arguments to file', async () => {
const mockWs = { write: jest.fn(), close: jest.fn() };
jest.spyOn(fs, 'createWriteStream').mockReturnValue(mockWs as any);
it('should throw error if ERROR is written to log', (done) => {
controller.deployApp(deploymentData).subscribe({
error: (err: BadRequestException) => {
expect(err).toBeInstanceOf(BadRequestException);
expect(err.message).toBe('my custom error');
// Ensure tail is properly "unwatched"
expect(onSubscription.closed).toBeTruthy();
done();
},
});

await firstValueFrom(controller.deployApp(deploymentData));
lines.next('some logs');
lines.next('ERROR my custom error');
});

expect(mockWs.write).toHaveBeenCalledWith(
'test-name de [email protected] test-username test-base y n',
);
expect(mockWs.close).toHaveBeenCalled();
it('should write arguments to file', (done) => {
controller.deployApp(deploymentData).subscribe(() => {
expect(mockWs.write).toHaveBeenCalledWith(
'test-name de [email protected] test-username test-base y n',
);
expect(mockWs.close).toHaveBeenCalled();
// Ensure tail is properly "unwatched"
expect(onSubscription.closed).toBeTruthy();
done();
});

lines.next('DONE');
});

it('should use the default locale if empty or omitted', async () => {
const mockWs = { write: jest.fn(), close: jest.fn() };
jest.spyOn(fs, 'createWriteStream').mockReturnValue(mockWs as any);
it('should use the default locale if empty', (done) => {
const emptyLocale = { ...deploymentData, locale: '' };
controller.deployApp(emptyLocale).subscribe(() => {
expect(mockWs.write).toHaveBeenCalledWith(
'test-name en [email protected] test-username test-base y n',
);
done();
});

const withoutLocale = { ...deploymentData, locale: '' };
await firstValueFrom(controller.deployApp(withoutLocale));
expect(mockWs.write).toHaveBeenCalledWith(
'test-name en [email protected] test-username test-base y n',
);
lines.next('DONE');
});

mockWs.write.mockReset();
delete withoutLocale.locale;
await firstValueFrom(controller.deployApp(withoutLocale));
expect(mockWs.write).toHaveBeenCalledWith(
'test-name en [email protected] test-username test-base y n',
);
it('should use the default locale if omitted', (done) => {
const noLocale = { ...deploymentData };
delete noLocale.locale;
controller.deployApp(noLocale).subscribe(() => {
expect(mockWs.write).toHaveBeenCalledWith(
'test-name en [email protected] test-username test-base y n',
);
done();
});

lines.next('DONE');
});
});
4 changes: 2 additions & 2 deletions src/app.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,14 @@ export class AppController {
if (line.startsWith('ERROR')) {
// Error found, text after error is returned
const message = line.replace('ERROR ', '');
tail.unwatch();
result.error(new BadRequestException(message));
result.complete();
tail.unwatch();
} else if (line.startsWith('DONE')) {
// Success, app is deployed
tail.unwatch();
result.next({ ok: true });
result.complete();
tail.unwatch();
}
});

Expand Down

0 comments on commit 17aa3ee

Please sign in to comment.