Skip to content

Commit

Permalink
Test: add and modify test
Browse files Browse the repository at this point in the history
add "mock" prefix"
add products controller test
  • Loading branch information
HC-kang committed Sep 28, 2023
1 parent 409a080 commit 9da0331
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 35 deletions.
2 changes: 0 additions & 2 deletions src/modules/auth/auth.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@ import { JwtService } from '@nestjs/jwt';

describe('AuthService', () => {
let service: AuthService;
let jwtService: JwtService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [JwtService, AuthService],
}).compile();

service = module.get<AuthService>(AuthService);
jwtService = module.get<JwtService>(JwtService);
});

it('should be defined', () => {
Expand Down
34 changes: 17 additions & 17 deletions src/modules/health-check/health-check.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,42 +12,42 @@ import {

describe('HealthCheckController', () => {
let controller: HealthCheckController;
let health: Partial<HealthCheckService>;
let http: Partial<HttpHealthIndicator>;
let typeOrm: Partial<TypeOrmHealthIndicator>;
let sample: Partial<SampleHealthIndicator>;
let mockHealth: Partial<HealthCheckService>;
let mockHttp: Partial<HttpHealthIndicator>;
let mockTypeOrm: Partial<TypeOrmHealthIndicator>;
let mockSample: Partial<SampleHealthIndicator>;

beforeEach(async () => {
health = {
mockHealth = {
check: jest.fn(),
};
http = {
mockHttp = {
pingCheck: jest.fn(),
};
typeOrm = {
mockTypeOrm = {
pingCheck: jest.fn(),
};
sample = {
mockSample = {
isHealthy: jest.fn(),
};
const module: TestingModule = await Test.createTestingModule({
controllers: [HealthCheckController],
providers: [
{
provide: HealthCheckService,
useValue: health,
useValue: mockHealth,
},
{
provide: HttpHealthIndicator,
useValue: http,
useValue: mockHttp,
},
{
provide: TypeOrmHealthIndicator,
useValue: typeOrm,
useValue: mockTypeOrm,
},
{
provide: SampleHealthIndicator,
useValue: sample,
useValue: mockSample,
},
],
}).compile();
Expand All @@ -61,7 +61,7 @@ describe('HealthCheckController', () => {

it('should call health check service', () => {
const check = jest.fn();
health.check = check;
mockHealth.check = check;
controller.check();
expect(check).toBeCalled();
});
Expand All @@ -73,17 +73,17 @@ describe('HealthCheckController', () => {
const sampleResult = { status: 'up' };

const httpSpy = jest
.spyOn(http, 'pingCheck')
.spyOn(mockHttp, 'pingCheck')
.mockResolvedValue(httpResult as unknown as HealthIndicatorResult);
const dbSpy = jest
.spyOn(typeOrm, 'pingCheck')
.spyOn(mockTypeOrm, 'pingCheck')
.mockResolvedValue(dbResult as unknown as HealthIndicatorResult);
const sampleSpy = jest
.spyOn(sample, 'isHealthy')
.spyOn(mockSample, 'isHealthy')
.mockResolvedValue(sampleResult as any);

const healthSpy = jest
.spyOn(health, 'check')
.spyOn(mockHealth, 'check')
.mockImplementation(
async (healthIndicators: HealthIndicatorFunction[]) => {
const results = await Promise.all(healthIndicators.map((cb) => cb()));
Expand Down
27 changes: 27 additions & 0 deletions src/modules/products/products.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Test, TestingModule } from '@nestjs/testing';
import { ProductsController } from './products.controller';
import { ProductsService } from './products.service';

describe('ProductsController', () => {
let controller: ProductsController;
let mockService: Partial<ProductsService>;

beforeEach(async () => {
mockService = {};
const module: TestingModule = await Test.createTestingModule({
controllers: [ProductsController],
providers: [
{
provide: ProductsService,
useValue: mockService,
},
],
}).compile();

controller = module.get<ProductsController>(ProductsController);
});

it('should be defined', () => {
expect(controller).toBeDefined();
});
});
12 changes: 6 additions & 6 deletions src/modules/products/products.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import { ProductsComponent } from './products.component';

describe('ProductsService', () => {
let service: ProductsService;
let comp: Partial<ProductsComponent>; // 가짜 컴포넌트 - partial 사용
let repo: ProductsRepositoryInterface; // 가짜 레포지토리 - interface 사용
let mockComp: Partial<ProductsComponent>; // 가짜 컴포넌트 - partial 사용
let mockRepo: ProductsRepositoryInterface; // 가짜 레포지토리 - interface 사용

beforeEach(async () => {
comp = {};
repo = {
mockComp = {};
mockRepo = {
doSomethingForProduct: jest.fn(),
create: jest.fn(),
all: jest.fn(),
Expand All @@ -23,8 +23,8 @@ describe('ProductsService', () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
ProductsService,
{ provide: 'ProductsRepositoryInterface', useValue: repo },
{ provide: ProductsComponent, useValue: comp },
{ provide: 'ProductsRepositoryInterface', useValue: mockRepo },
{ provide: ProductsComponent, useValue: mockComp },
],
}).compile();

Expand Down
13 changes: 10 additions & 3 deletions src/modules/users/users.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
import { Test, TestingModule } from '@nestjs/testing';
import { UsersController } from './users.controller';
import { UsersService } from './users.service';
import { AuthService } from '../auth/auth.service';

describe('UsersController', () => {
let controller: UsersController;
let service: Partial<UsersService>;
let mockService: Partial<UsersService>;
let mockAuthService: Partial<UsersService>;

beforeEach(async () => {
service = {};
mockService = {};
mockAuthService = {};
const module: TestingModule = await Test.createTestingModule({
controllers: [UsersController],
providers: [
{
provide: UsersService,
useValue: service,
useValue: mockService,
},
{
provide: AuthService,
useValue: mockAuthService,
},
],
}).compile();
Expand Down
13 changes: 6 additions & 7 deletions src/modules/users/users.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ import { AuthService } from '../auth/auth.service';

describe('UsersService', () => {
let service: UsersService;
let logger: Logger;
let authService: Partial<AuthService>;
let repo: UsersRepositoryInterface;
let mockAuthService: Partial<AuthService>;
let mockRepo: UsersRepositoryInterface;

beforeEach(async () => {
authService = {};
repo = {
mockAuthService = {};
mockRepo = {
create: jest.fn(),
createMany: jest.fn(),
all: jest.fn(),
Expand All @@ -28,11 +27,11 @@ describe('UsersService', () => {
UsersService,
{
provide: AuthService,
useValue: authService,
useValue: mockAuthService,
},
{
provide: 'UsersRepositoryInterface',
useValue: repo,
useValue: mockRepo,
},
],
}).compile();
Expand Down

0 comments on commit 9da0331

Please sign in to comment.