Skip to content

Commit

Permalink
test: proper deps mocks in jest unit tests, add unit tests to ci (#364)
Browse files Browse the repository at this point in the history
  • Loading branch information
pmstss authored Aug 27, 2024
1 parent c81af49 commit fc02e06
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 98 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,6 @@ jobs:

- name: Build
run: npm run build

- name: Test
run: npm run test
96 changes: 5 additions & 91 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
"@nestjs/testing": "^9.4.3",
"@types/axios": "^0.14.0",
"@types/dot": "^1.1.5",
"@types/jest": "^26.0.24",
"@types/jest": "^29.5.12",
"@types/jsonwebtoken": "^8.5.9",
"@types/jwk-to-pem": "^2.0.1",
"@types/jwt-simple": "^0.5.33",
Expand All @@ -94,7 +94,7 @@
"typescript": "^4.7.4"
},
"jest": {
"testTimeout": 3000000,
"testTimeout": 120000,
"setupFiles": [
"dotenv/config"
],
Expand Down
17 changes: 17 additions & 0 deletions src/auth/auth.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
import { Test, TestingModule } from '@nestjs/testing';
import { KeyCloakService } from '../keycloak/keycloak.service';
import { UsersService } from '../users/users.service';
import { AuthController } from './auth.controller';
import { AuthService } from './auth.service';

describe('AuthController', () => {
let controller: AuthController;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [AuthController],
providers: [
{
provide: UsersService,
useValue: {},
},
{
provide: KeyCloakService,
useValue: {},
},
{
provide: AuthService,
useValue: {},
},
],
}).compile();

controller = module.get<AuthController>(AuthController);
Expand Down
33 changes: 32 additions & 1 deletion src/auth/auth.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,43 @@
import * as fs from 'fs';
import { EntityManager } from '@mikro-orm/core';
import { ConfigService } from '@nestjs/config';
import { Test, TestingModule } from '@nestjs/testing';
import { HttpClientService } from '../httpclient/httpclient.service';
import { KeyCloakService } from '../keycloak/keycloak.service';
import { AuthService } from './auth.service';

jest.mock('fs');

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

beforeEach(async () => {
(fs.readFileSync as jest.Mock) = jest.fn((path: string) =>
path.toLowerCase().includes('json') ? '{}' : 'mocked-key-content',
);

const module: TestingModule = await Test.createTestingModule({
providers: [AuthService],
providers: [
AuthService,
{
provide: EntityManager,
useValue: {},
},
{
provide: KeyCloakService,
useValue: {},
},
{
provide: HttpClientService,
useValue: {},
},
{
provide: ConfigService,
useValue: {
get: (key: string) => key,
},
},
],
}).compile();

service = module.get<AuthService>(AuthService);
Expand Down
7 changes: 7 additions & 0 deletions src/file/file.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import { Test, TestingModule } from '@nestjs/testing';
import { FileController } from './file.controller';
import { FileService } from './file.service';

describe('FileController', () => {
let controller: FileController;

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

controller = module.get<FileController>(FileController);
Expand Down
12 changes: 12 additions & 0 deletions src/testimonials/testimonials.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
import { Test, TestingModule } from '@nestjs/testing';
import { AuthService } from '../auth/auth.service';
import { TestimonialsController } from './testimonials.controller';
import { TestimonialsService } from './testimonials.service';

describe('TestimonialsController', () => {
let controller: TestimonialsController;

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

controller = module.get<TestimonialsController>(TestimonialsController);
Expand Down
15 changes: 14 additions & 1 deletion src/testimonials/testimonials.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
import { EntityManager } from '@mikro-orm/core';
import { getRepositoryToken } from '@mikro-orm/nestjs';
import { Test, TestingModule } from '@nestjs/testing';
import { Testimonial } from './api/testimonial.model';
import { TestimonialsService } from './testimonials.service';

describe('TestimonialsService', () => {
let service: TestimonialsService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [TestimonialsService],
providers: [
TestimonialsService,
{
provide: EntityManager,
useValue: {},
},
{
provide: getRepositoryToken(Testimonial),
useValue: {},
},
],
}).compile();

service = module.get<TestimonialsService>(TestimonialsService);
Expand Down
17 changes: 17 additions & 0 deletions src/users/users.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
import { Test, TestingModule } from '@nestjs/testing';
import { AuthService } from '../auth/auth.service';
import { KeyCloakService } from '../keycloak/keycloak.service';
import { UsersController } from './users.controller';
import { UsersService } from './users.service';

describe('UsersController', () => {
let controller: UsersController;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [UsersController],
providers: [
{
provide: UsersService,
useValue: {},
},
{
provide: KeyCloakService,
useValue: {},
},
{
provide: AuthService,
useValue: {},
},
],
}).compile();

controller = module.get<UsersController>(UsersController);
Expand Down
12 changes: 9 additions & 3 deletions src/users/users.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
import { getRepositoryToken } from '@mikro-orm/nestjs';
import { Test, TestingModule } from '@nestjs/testing';
import { HttpClientService } from '../httpclient/httpclient.service';
import { KeyCloakService } from '../keycloak/keycloak.service';
import { User } from '../model/user.entity';
import { UsersService } from './users.service';

describe('UsersService', () => {
let service: UsersService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [UsersService, KeyCloakService, HttpClientService],
providers: [
UsersService,
{
provide: getRepositoryToken(User),
useValue: {},
},
],
}).compile();

service = module.get<UsersService>(UsersService);
Expand Down

0 comments on commit fc02e06

Please sign in to comment.