Skip to content

Commit

Permalink
fix e2e test
Browse files Browse the repository at this point in the history
  • Loading branch information
RusDyn committed Mar 6, 2023
1 parent 5922064 commit e50273d
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 11 deletions.
24 changes: 24 additions & 0 deletions src/app.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { AppController } from './app.controller';
import { OpenAIService } from './openai.service';

describe('CatsController', () => {
let appController: AppController;
let openai: OpenAIService;

beforeEach(() => {
openai = new OpenAIService();
appController = new AppController(openai);
});

describe('sendMessage', () => {
it('should return an message', async () => {
const text = 'Hello World!';
const result = { text };
jest.spyOn(openai, 'sendMessage').mockImplementation(async () => (result));

expect(await appController.sendMessage({
text: 'Hello World!'
})).toBe(result);
});
});
});
2 changes: 1 addition & 1 deletion src/app.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export class AppController {

@Post("/sendMessage")
@UseGuards(SecretKeyGuard)
async getStatus(@Body() body) {
async sendMessage(@Body() body) {

const { text: message, conversationId } = body;

Expand Down
38 changes: 28 additions & 10 deletions test/app.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,32 @@
import { Test, TestingModule } from '@nestjs/testing';
import { INestApplication } from '@nestjs/common';
import { INestApplication, ValidationPipe } from '@nestjs/common';
import * as request from 'supertest';
import { AppModule } from '../src/app.module';
import * as dotenv from 'dotenv';
import { OpenAIService } from '../src/openai.service';


describe('AppModule (e2e)', () => {
let app: INestApplication;
let server: any = null;
let agent: request.SuperAgentTest;
let openAiService = {
sendMessage: async (text) => {

return { text, id: '123' }
}
};

beforeEach(async () => {
dotenv.config();

const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule],
}).compile();
})
.overrideProvider(OpenAIService)
.useValue(openAiService)
.compile();

app = await moduleFixture.createNestApplication().init();
app = moduleFixture.createNestApplication()
await app.init();
server = await app.listen(4000);
// Since the application is already listening, it should use the allocated port
agent = request.agent(server);
Expand All @@ -28,14 +38,22 @@ describe('AppModule (e2e)', () => {
});

describe('Service OpenAI', () => {
it('GET: /sendMessage', async () => {
it('POST: /sendMessage', async () => {
const TEST_MESSAGE = 'Hello!';

const response = await agent
.get(`/sendMessage?text=${TEST_MESSAGE}`)
.expect(200);
const res = await agent
.post(`/sendMessage`)
.set('Content-Type', 'application/json')
.send({
text: TEST_MESSAGE,

});

expect(response.text).toBeTruthy();
expect(res.status).toBe(201)
expect(res.body).toBeTruthy();
expect(res.body.id).toBe('123');
expect(res.body.text).toBe(TEST_MESSAGE);
//console.log(res);
});
});
});

0 comments on commit e50273d

Please sign in to comment.