Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix tests #322

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions src/modules/auth/auth.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,46 @@
import type { TestingModule } from '@nestjs/testing';
import { Test } from '@nestjs/testing';

import { UserEntity } from '../user/user.entity';
import { UserService } from '../user/user.service';
import { AuthController } from './auth.controller';
import { AuthService } from './auth.service';
import { TokenPayloadDto } from './dto/TokenPayloadDto';

describe('AuthController', () => {
const defaultUser = new UserEntity();

let app: TestingModule;

beforeAll(async () => {
app = await Test.createTestingModule({
controllers: [AuthController],
providers: [],
providers: [
{
provide: AuthService,
useValue: {
createAccessToken: () =>
Promise.resolve(
new TokenPayloadDto({ expiresIn: 3600, accessToken: 'token' }),
),
validateUser: () => Promise.resolve(defaultUser),
},
},
{
provide: UserService,
useValue: {
createUser: () => Promise.resolve(defaultUser),
},
},
],
imports: [],
}).compile();
});

describe('root', () => {
it('should return "http://localhost"', () => {
it('should be defined', () => {
const appController = app.get<AuthController>(AuthController);
expect(appController).toBe('http://localhost');
expect(appController).toBeDefined();
});
});
});
52 changes: 52 additions & 0 deletions src/modules/user/user.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import type { TestingModule } from '@nestjs/testing';
import { Test } from '@nestjs/testing';

import { PageDto } from '../../common/dto/page.dto';
import { PageMetaDto } from '../../common/dto/page-meta.dto';
import { TranslationService } from '../../shared/services/translation.service';
import { UsersPageOptionsDto } from './dtos/users-page-options.dto';
import { UserController } from './user.controller';
import { UserEntity } from './user.entity';
import { UserService } from './user.service';

describe('UserController', () => {
let app: TestingModule;
let underTest: UserController;
let userService: UserService;

beforeAll(async () => {
app = await Test.createTestingModule({
controllers: [UserController],
providers: [
{ provide: TranslationService, useValue: {} },
{
provide: UserService,
useValue: {
getUsers: (pageOptionsDto: UsersPageOptionsDto) =>
Promise.resolve(
new PageDto(
[new UserEntity()],
new PageMetaDto({ pageOptionsDto, itemCount: 1 }),
),
),
},
},
],
}).compile();

underTest = app.get<UserController>(UserController);
userService = app.get<UserService>(UserService);
});

it('returns users', async () => {
// setup
const serviceGetUsers = jest.spyOn(userService, 'getUsers');

// run
const result = await underTest.getUsers(new UsersPageOptionsDto());

// verify
expect(result.data).toHaveLength(1);
expect(serviceGetUsers).toHaveBeenCalledTimes(1);
});
});
6 changes: 1 addition & 5 deletions tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
{
"extends": "./tsconfig.json",
"exclude": [
"node_modules",
"test",
"**/*spec.ts"
]
"exclude": ["node_modules"]
}
9 changes: 2 additions & 7 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@
"incremental": true,
"strictNullChecks": true
},
"include": [
"src/**/*"
],
"exclude": [
"node_modules",
"**/*.spec.ts"
]
"include": ["src/**/*"],
"exclude": ["node_modules"]
}