Skip to content

Commit

Permalink
chore: Add test application
Browse files Browse the repository at this point in the history
  • Loading branch information
unlight committed Jan 25, 2025
1 parent 0835e39 commit 25921e7
Show file tree
Hide file tree
Showing 9 changed files with 190 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { Logger } from '@nestjs/common';
import { NestologModule } from 'nestolog';

@Module({
imports: [NestologModule.forRoot(options)],
imports: [NestologModule.register(options)],
})
export class AppModule {}
```
Expand Down
18 changes: 18 additions & 0 deletions app/app.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { setTimeout } from 'node:timers/promises';

import { Controller, Get, Logger } from '@nestjs/common';

import { AppService } from './app.service';

@Controller()
export class AppController {
private logger = new Logger('AppController');
constructor(private readonly appService: AppService) {}

@Get('/')
async index() {
this.logger.debug('Start index');
await setTimeout(10); // Imitate running process
this.appService.addToShared(new Date().toUTCString());
}
}
84 changes: 84 additions & 0 deletions app/app.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { INestMicroservice, Provider } from '@nestjs/common';
import {
ClientKafka,
ClientProxyFactory,
Transport,
} from '@nestjs/microservices';
import { Test } from '@nestjs/testing';
import expect from 'expect';
import { createAwaitableEmit } from '../src';
import { AppModule, configureApp } from './app.module';
import { AppService } from './app.service';

describe('AppController (e2e)', () => {
let app: INestMicroservice;
let clientKafka: ClientKafka;
let service: AppService;

const { emitMessage, AwaitableEmitInterceptor, dispose } =
createAwaitableEmit({
getKafkaClient: () => clientKafka,
brokers: ['127.0.0.1:9092'],
});

before(async () => {
const providers: Provider[] = [
{
provide: 'KAFKA_CLIENT',
useFactory: () => {
return ClientProxyFactory.create({
transport: Transport.KAFKA,
options: {
client: {
clientId: 'KAFKA_CLIENT',
brokers: ['127.0.0.1:9092'],
},
},
});
},
},
];
const testingModule = await Test.createTestingModule({
imports: [AppModule],
providers,
}).compile();

app = testingModule.createNestMicroservice({
transport: Transport.KAFKA,
options: {
client: {
clientId: 'KAFKA_CLIENT',
brokers: ['127.0.0.1:9092'],
},
},
});

configureApp(app);

app.useGlobalInterceptors(new AwaitableEmitInterceptor());

await app.init();
await app.listen();

clientKafka = app.get<ClientKafka>('KAFKA_CLIENT');
service = app.get(AppService);
});

after(async () => {
await dispose();
await clientKafka?.close();
await app?.close();
});

it('smoke', () => {
expect(clientKafka).toBeTruthy();
});

it('test emit message', async () => {
await emitMessage('user-created', {
key: Date.now.toString(),
value: { name: 'Bob' },
});
expect(service.shared.at(-1)).toEqual({ name: 'Bob' });
});
});
44 changes: 44 additions & 0 deletions app/app.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import {
INestApplication,
INestMicroservice,
Logger,
Module,
} from '@nestjs/common';

import { NestologModule } from '../src';
import { AppController } from './app.controller';
import { AppService } from './app.service';

@Module({
controllers: [AppController],
imports: [NestologModule.register({})],
providers: [AppService],
})
export class AppModule {}

export function configureApp(app: INestApplication | INestMicroservice) {
const oldfn = app.registerRequestByContextId.bind(app);

app.registerRequestByContextId = function (req, contextId) {
console.log('contextId', contextId);
debugger;
return oldfn.apply(this, arguments as any);
};

if ('use' in app && typeof app.use === 'function') {
// not alwasy may be handled
// const server = app.getHttpServer();
// server.on('request', function (req, res) {
// debugger;
// });

app.use(function (req, res, next) {
// debugger;
next();
});
}

const logger = app.get(Logger);

logger.log('Application configured');
}
20 changes: 20 additions & 0 deletions app/app.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Injectable } from '@nestjs/common';

import { NestoLogger } from '../src';

@Injectable()
export class AppService {
constructor(private logger: NestoLogger) {}

shared: string[] = [];

addToShared(value: string) {
this.shared.push(value);

this.logger.debug(`Value to shared added '${value}'`, 'AppService');
}

getHello(): string {
return 'Hello World! ' + new Date().toUTCString();
}
}
17 changes: 17 additions & 0 deletions app/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { NestFactory } from '@nestjs/core';

import { NestoLogger } from '../src';
import { AppModule, configureApp } from './app.module';

async function main() {
const app = await NestFactory.create(AppModule, {
logger: NestoLogger.create(),
});
configureApp(app);

await app.init();
await app.listen(3000);
}

// eslint-disable-next-line @typescript-eslint/no-floating-promises
main();
4 changes: 2 additions & 2 deletions src/test-app.ts → app/test-app.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Injectable, Logger, Module } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';

import { NestologModule } from './nestolog.module';
import { NestoLogger } from './nestologger.service';
import { NestologModule } from '../src/nestolog.module';
import { NestoLogger } from '../src/nestologger.service';

const loggerModule = NestologModule.register({});

Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
},
"homepage": "https://github.com/unlight/nestolog#readme",
"scripts": {
"testapp": "ts-node-dev app/main.ts",
"testmain": "ts-node app/main.ts",
"testmain:dev": "ts-node-dev --inspect -- app/main.ts",
"testapp": "ts-node app/test-app.ts",
"test": "npm run eslint && npm run tscheck && npm run test:cov",
"test:r": "vitest run src",
"test:cov": "vitest run src --coverage",
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
}
},
"compilerOptions": {
"target": "ES2020",
"target": "ES2022",
"module": "commonjs",
"moduleResolution": "node",
"importHelpers": true,
Expand Down

0 comments on commit 25921e7

Please sign in to comment.