Skip to content
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
85 changes: 85 additions & 0 deletions sample/10-fastify/e2e/cats.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { Test, TestingModule } from '@nestjs/testing';
import { INestApplication } from '@nestjs/common';
import * as request from 'supertest';
import { CatsModule } from '../src/cats/cats.module';
import {
FastifyAdapter,
NestFastifyApplication,
} from '@nestjs/platform-fastify';
import { CreateCatDto } from '../src/cats/dto/create-cat.dto';
import { RolesGuard } from '../src/common/guards/roles.guard';

describe('CatsController (e2e)', () => {
let app: INestApplication;

const mockRolesGuard = { canActivate: () => true };

beforeAll(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [CatsModule],
})
.overrideGuard(RolesGuard)
.useValue(mockRolesGuard)
Comment on lines +21 to +22
Copy link
Contributor

@Sikora00 Sikora00 Jan 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’m not sure it’s a good idea to mock a guard in an e2e test. It feels a bit like skipping part of the controller or endpoint behavior without clearly explaining why.

I also see that this can’t really work as written, since it operates on a user that’s never attached to the request. Maybe we could either introduce a middleware that handles this, or remove the guard from the example altogether.

.compile();

app = moduleFixture.createNestApplication<NestFastifyApplication>(
new FastifyAdapter(),
);

await app.init();
await app.getHttpAdapter().getInstance().ready();
});

afterAll(async () => {
await app.close();
});

it('/cats (GET) - initial state', () => {
return request(app.getHttpServer())
.get('/cats')
.expect(200)
.expect([]);
});

it('/cats (POST) - create a cat', () => {
const newCatDto: CreateCatDto = { name: 'Milo', age: 2, breed: 'Tabby' };
const expectedCat = {
id: 1,
...newCatDto,
};

return request(app.getHttpServer())
.post('/cats')
.send(newCatDto)
.expect(201)
.expect(expectedCat);
});

it('/cats (GET) - after create', () => {
const expectedCat = {
id: 1,
name: 'Milo',
age: 2,
breed: 'Tabby',
};

return request(app.getHttpServer())
.get('/cats')
.expect(200)
.expect([expectedCat]);
});

it('/cats/:id (GET) - find one', () => {
const expectedCat = {
id: 1,
name: 'Milo',
age: 2,
breed: 'Tabby',
};

return request(app.getHttpServer())
.get('/cats/1')
.expect(200)
.expect(expectedCat);
});
});
9 changes: 9 additions & 0 deletions sample/10-fastify/e2e/jest-e2e.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"moduleFileExtensions": ["js", "json", "ts"],
"rootDir": ".",
"testEnvironment": "node",
"testRegex": ".e2e-spec.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
}
}
7 changes: 7 additions & 0 deletions sample/10-fastify/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
rootDir: 'src',
testRegex: '.*\\.spec\\.ts$',
moduleFileExtensions: ['js', 'json', 'ts'],
};
2 changes: 1 addition & 1 deletion sample/10-fastify/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "echo 'No e2e tests implemented yet.'"
"test:e2e": "jest --config ./e2e/jest-e2e.json"
},
"dependencies": {
"@nestjs/common": "11.1.9",
Expand Down
38 changes: 23 additions & 15 deletions sample/10-fastify/src/cats/cats.controller.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,40 @@
import { Body, Controller, Get, Param, Post, UseGuards } from '@nestjs/common';
import { Roles } from '../common/decorators/roles.decorator';
import { RolesGuard } from '../common/guards/roles.guard';
import { ParseIntPipe } from '../common/pipes/parse-int.pipe';
import { CatsService } from './cats.service';
import {
Controller,
Get,
Post,
Body,
Param,
UseGuards,
NotFoundException,
} from '@nestjs/common';
import { CreateCatDto } from './dto/create-cat.dto';
import { CatsService } from './cats.service';
import { RolesGuard } from '../common/guards/roles.guard';
import { Roles } from '../common/decorators/roles.decorator';
import { Cat } from './interfaces/cat.interface';

@UseGuards(RolesGuard)
@Controller('cats')
@UseGuards(RolesGuard)
export class CatsController {
constructor(private readonly catsService: CatsService) {}

@Post()
@Roles('admin')
async create(@Body() createCatDto: CreateCatDto) {
this.catsService.create(createCatDto);
create(@Body() createCatDto: CreateCatDto): Cat {
return this.catsService.create(createCatDto);
}

@Get()
async findAll(): Promise<Cat[]> {
findAll(): Cat[] {
return this.catsService.findAll();
}

@Get(':id')
findOne(
@Param('id', new ParseIntPipe())
id: number,
) {
// get by ID logic
findOne(@Param('id') id: string): Cat {
const cat = this.catsService.findOne(+id);
if (!cat) {
throw new NotFoundException(`Cat with ID ${id} not found`);
}
return cat;
}
}
}
57 changes: 57 additions & 0 deletions sample/10-fastify/src/cats/cats.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { Test, TestingModule } from '@nestjs/testing';
import { CatsService } from './cats.service';
import { Cat } from './interfaces/cat.interface';
import { CreateCatDto } from './dto/create-cat.dto';

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

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

service = module.get<CatsService>(CatsService);
});

it('should be defined', () => {
expect(service).toBeDefined();
});

describe('create and findAll', () => {
it('should create a new cat, assign an ID, and return all cats', () => {
const catDto: CreateCatDto = { name: 'Milo', age: 2, breed: 'Tabby' };

const createdCat = service.create(catDto);

expect(createdCat.id).toBe(1);
expect(createdCat.name).toBe('Milo');

const allCats = service.findAll();

expect(allCats).toHaveLength(1);
expect(allCats).toEqual([createdCat]);

const catDto2: CreateCatDto = { name: 'Luna', age: 1, breed: 'Siamese' };
const createdCat2 = service.create(catDto2);
expect(createdCat2.id).toBe(2);
});
});

describe('findOne', () => {
it('should return a single cat by its id', () => {
const catDto: CreateCatDto = { name: 'Luna', age: 1, breed: 'Siamese' };
const createdCat = service.create(catDto);

const foundCat = service.findOne(1);

expect(foundCat).toBeDefined();
expect(foundCat).toEqual(createdCat);
});

it('should return undefined if cat is not found', () => {
const foundCat = service.findOne(999);
expect(foundCat).toBeUndefined();
});
});
});
18 changes: 15 additions & 3 deletions sample/10-fastify/src/cats/cats.service.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
import { Injectable } from '@nestjs/common';
import { Cat } from './interfaces/cat.interface';
import { CreateCatDto } from './dto/create-cat.dto';

@Injectable()
export class CatsService {
private readonly cats: Cat[] = [];
private idCounter = 1;

create(cat: Cat) {
this.cats.push(cat);
create(createCatDto: CreateCatDto): Cat {
const newCat: Cat = {
id: this.idCounter++,
...createCatDto,
};

this.cats.push(newCat);
return newCat;
}

findAll(): Cat[] {
return this.cats;
}
}

findOne(id: number): Cat {
return this.cats.find((cat) => cat.id === id);
}
}
1 change: 1 addition & 0 deletions sample/10-fastify/src/cats/interfaces/cat.interface.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export interface Cat {
readonly id: number;
readonly name: string;
readonly age: number;
readonly breed: string;
Expand Down
6 changes: 5 additions & 1 deletion sample/10-fastify/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,9 @@
"incremental": true,
"skipLibCheck": true
},
"include": ["src/**/*"]
"include": ["src/**/*"],
"types": [
"node",
"jest",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wouldn't a tsconfig.spec.ts be better?

]
}