configure unit tests

This commit is contained in:
Joseph Ditton 2021-12-05 18:46:44 -07:00
parent 8ca1046bf3
commit cc0f32a75f
3 changed files with 74 additions and 4 deletions

View File

@ -89,7 +89,10 @@
"json",
"ts"
],
"moduleDirectories": ["node_modules", "./"],
"moduleNameMapper": {
"^server/(.*)$": "<rootDir>/$1"
},
"moduleDirectories": ["node_modules"],
"rootDir": "./server",
"testRegex": ".*\\.spec\\.ts$",
"transform": {

View File

@ -13,9 +13,9 @@ describe('AppController', () => {
appController = app.get<AppController>(AppController);
});
describe('root', () => {
it('should return "Hello World!"', () => {
expect(appController.index()).toBe('Hello World!');
describe('index', () => {
it('should run without crashing', () => {
expect(appController.index()).toBeUndefined();
});
});
});

View File

@ -0,0 +1,67 @@
import 'server/env';
import { UsersController } from './users.controller';
import { UsersService } from 'server/providers/services/users.service';
import { JwtService } from 'server/providers/services/jwt.service';
import { RefreshTokensService } from 'server/providers/services/refresh_tokens.service';
import { Test, TestingModule } from '@nestjs/testing';
import { RolesService } from 'server/providers/services/roles.service';
import { Response } from 'express';
import { User } from 'server/entities/user.entity';
describe('UsersController', () => {
let usersController: UsersController;
const res = { cookie: () => {} };
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [UsersController],
providers: [
{
provide: RefreshTokensService,
useValue: {
create: async (refreshToken) => refreshToken,
},
},
{
provide: UsersService,
useValue: {
findAll: async () => [{ id: 1 }],
create: async (user: User) => user,
},
},
{
provide: RolesService,
useValue: {
findByKey: (key) => [{ key, id: 1 }],
},
},
JwtService,
],
}).compile();
usersController = module.get<UsersController>(UsersController);
});
describe('index', () => {
it('should get all users', async () => {
expect(await usersController.index()).toEqual({ users: [{ id: 1 }] });
});
});
describe('create', () => {
it('should create a user with email and password', async () => {
const { token, user } = await usersController.create(
{ firstName: 'firstName', lastName: 'lastName', email: 'test@test.com', password: 'test' },
res as unknown as Response<any>,
);
expect(token.length).toBeGreaterThan(0);
expect(user.email).toEqual('test@test.com');
expect(user.firstName).toEqual('firstName');
expect(user.lastName).toEqual('lastName');
expect(user.passwordHash.length).toBeGreaterThan(0);
expect(user.passwordHash).not.toEqual('test');
expect(user.userRoles).toHaveLength(1);
expect(user.userRoles[0].role.key).toEqual('user');
});
});
});