Skip to content

Instantly share code, notes, and snippets.

@nishabe
Last active January 7, 2021 15:08
Show Gist options
  • Save nishabe/edf482da9dcdb9fe9349f853afb9cc0f to your computer and use it in GitHub Desktop.
Save nishabe/edf482da9dcdb9fe9349f853afb9cc0f to your computer and use it in GitHub Desktop.
student.service.spec.ts
import { Test, TestingModule } from '@nestjs/testing';
import { StudentService } from './student.service';
import { ApiService } from '../api/api.service';
class ApiServiceMock {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
getStudent(_firstName: string, _lastName: string) {
return {
name: 'Jane Doe',
grades: [3.7, 3.8, 3.9, 4.0, 3.6],
};
}
}
describe('StudentService', () => {
let studentService: StudentService;
beforeEach(async () => {
const ApiServiceProvider = {
provide: ApiService,
useClass: ApiServiceMock,
};
const module: TestingModule = await Test.createTestingModule({
providers: [StudentService, ApiServiceProvider],
}).compile();
studentService = module.get<StudentService>(StudentService);
});
it('StudentService - should be defined', () => {
expect(studentService).toBeDefined();
});
describe('getGpa', () => {
it('should get student GPA', async () => {
const expectedGpa = 3.8;
const gpa = await studentService.getGpa('Jane', 'Doe');
expect(gpa).toEqual(expectedGpa);
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment