Skip to content

Commit

Permalink
test test
Browse files Browse the repository at this point in the history
  • Loading branch information
mhd-hi committed Jul 25, 2024
1 parent 64883da commit a616c7c
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 10 deletions.
1 change: 0 additions & 1 deletion .vscode/extensions.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"recommendations": [
"dbaeumer.vscode-eslint",
"esbenp.prettier-vscode",
"rvest.vs-code-prettier-eslint",
"abians.prisma-generate-uml"
]
}
83 changes: 74 additions & 9 deletions test/course/course.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,85 @@
import { INestApplication, ValidationPipe } from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing';
import { Course, Session } from '@prisma/client';
import { useContainer } from 'class-validator';
import * as request from 'supertest';

import { CourseService } from '../../src/course/course.service';
import { AppModule } from '../../src/app.module';
import { PrismaService } from '../../src/prisma/prisma.service';

describe('CourseService', () => {
let service: CourseService;
describe('CourseService (e2e)', () => {
let app: INestApplication;
let prisma: PrismaService;
let course: Course;
let session: Session;

Check failure on line 14 in test/course/course.service.spec.ts

View workflow job for this annotation

GitHub Actions / build-and-test (18.x)

'session' is assigned a value but never used

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [PrismaService, CourseService],
const courseShape = expect.objectContaining({
id: expect.any(String),
programId: expect.any(String),
code: expect.any(String),
title: expect.any(String),
description: expect.any(String),
credits: expect.any(Number),
});

beforeAll(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule],
}).compile();

service = module.get<CourseService>(CourseService);
app = moduleFixture.createNestApplication();
prisma = app.get<PrismaService>(PrismaService);

useContainer(app.select(AppModule), { fallbackOnErrors: true });
app.useGlobalPipes(new ValidationPipe({ whitelist: true }));

await app.init();

session = await prisma.session.create({
data: {
trimester: 'A',
year: 2021,
},
});

course = await prisma.course.create({
data: {
id: '1',
programId: '1',
code: 'CS101',
title: 'Introduction to Computer Science',
description: 'A basic course on computer science',
credits: 3,
},
});
});

it('should be defined', () => {
expect(service).toBeDefined();
afterAll(async () => {
await prisma.course.deleteMany();
await prisma.session.deleteMany();
await prisma.$disconnect();
await app.close();
});

describe('GET /courses', () => {
it('returns a list of courses', async () => {
const { status, body } = await request(app.getHttpServer()).get(
'/courses',
);

expect(status).toBe(200);
expect(body).toStrictEqual(expect.arrayContaining([courseShape]));
});
});

describe('GET /courses/:id', () => {
it('returns a course', async () => {
const { status, body } = await request(app.getHttpServer()).get(
`/courses/${course.id}`,
);

expect(status).toBe(200);
expect(body).toStrictEqual(courseShape);
});
});
});

0 comments on commit a616c7c

Please sign in to comment.